简体   繁体   English

如何正确存储多个实例对象?

[英]How would I store multiple instance objects correctly?

I am making an intermediate/advanced C++ program, a video game to be exact. 我正在制作一个中级/高级C ++程序,准确的视频游戏。

Lately I have been noticing that there is a good amount of memory being leaked, and I was wondering if maybe something is wrong with the way I am creating my instances. 最近我注意到有大量的内存被泄露,我想知道我创建实例的方式是否有问题。

Below is a summarized(but originally complex) class: 下面是一个总结(但最初是复杂的)类:

class theObject
{
 //Instance variables
 //Instance functions
};

With this object(along with any other objects that I am storing, I have an array index of every different variation template of theObject . That part is not important, but the way I am storing them(or in my opinion) is: 与此对象(与我存储任何其他的目的,我的每一个不同的变异模板的数组索引theObject这部分不是很重要,但是我将它们存储(或者在我看来的方式)是:

//NEWER VERSION WITH MORE INFO
void spawnTheObject()
{
 theObject* NewObj=ObjectArray[N];
 //I give the specific copy its individual parameters(such as its spawn location and few edited stats)
 NewObj->giveCustomStats(int,int,int,int);//hard-coded, not actual params
 NewObj->Spawn(float,float,float);
 myStorage.push_back(new theObject(*NewObj));
}


//OLDER VERSION
void spawnTheObject()
    {
     //create a copy of the arrayed object
     theObject* NewObj=new theObject(*ObjectArray[N]);
     //spawn the object(in this case it could be a monster), and I am spawning multiple copies of them obviously
     //then store into the storage object(currently a deque(originally a vector))
     myStorage.push_back(new theObject(*NewObj));
     //and delete the temporary one
     delete NewObj;
    }

I am currently using a deque(recently changed from using a vector) but I am seeing no difference in the memory usage. 我目前正在使用deque(最近使用向量更改)但我发现内存使用没有区别。 I have though found out from "comment tests" that these spawning functions I have are the reason for the memory leaks. 我从“评论测试”中发现,我所拥有的这些产卵函数是内存泄漏的原因。 Since this is the wrong way to create/spawn the instances, I was wondering if there is a better way to storing these objects. 由于这是创建/生成实例的错误方法,我想知道是否有更好的方法来存储这些对象。

tl;dr: What are some better objects to store a non-constant amount of objects and how? tl; dr:有哪些更好的对象来存储非恒定数量的对象以及如何?

I am guessing you never clear new spawn objects in myStorage that causes the memory to increase(as you refer to memory leak). 我猜你永远不会在myStorage中清除新的spawn对象导致内存增加(因为你引用内存泄漏)。 If I am correct your myStorage is declared as below: 如果我是正确的,你的myStorage声明如下:

std::deque<theObject*> myStorage;

if you call either of below calls, the pointers to theObject are delete but the real dynamically allocated objects are not deleted. 如果您调用以下任一调用,则指向该对象的指针将被删除,但不会删除实际动态分配的对象。

 myStorage.pop_back();
 myStorage.clear();

Another small issue in your code, you are making unnecessary object allocate/dellocate in spawnTheObject() function. 您的代码中的另一个小问题是,您在spawnTheObject()函数中进行不必要的对象分配/解除spawnTheObject()

How to clean container with pointer type 如何用指针类型清理容器

You need to iterate through each element in myStorage, delete the object then clear the container, for example: 您需要遍历myStorage中的每个元素,删除该对象然后清除容器,例如:

for (std::deque<theObject*>::iterator iter=myStorage.begin();
     iter != myStorage.end(); ++iter)
{
   delete (*iter);
}
myStorage.clear();

Better solution: 更好的方案:

Use smart pointers in std::deque or std::vector , then when you remove an element from STL container the object the pointer is pointing to is also deleted automatically. std::dequestd::vector使用智能指针,然后当从STL容器中删除元素时,指针指向的对象也会自动删除。

 #include <memory>

 std::deque<std::shared_ptr<theObject> > myStorage;
 myStorage.push_back(std::shared_ptr<theObject>(new *ObjectArray[N]));

 mySorage.clear();  // all memories cleared properly, no worries

If you do not manually delete your objects from myStorage at the end of the game or when they need to be destroyed, there is your memory leak. 如果你没有在游戏结束时从myStorage手动删除你的对象,或者当它们需要被销毁时,你的内存就会泄漏。

myStorage.push_back(new theObject(*NewObj));

The object being pushed into storage is allocated by you, so it should be destroyed by you when it needs to disappear. 被推入存储的对象由您分配,因此当它需要消失时,它应该被您销毁。

Also I do not understand the need for the intermediate NewObj object, it isnt a memory leak but it is an unnecessary performance cost, 1 allocation/deallocation + 1 copy. 另外我不了解中间NewObj对象的需要,它不是内存泄漏,但它是一个不必要的性能成本,1分配/释放+ 1副本。

As Forever mentioned, your best bet is to start using smart pointers, either std::unique_ptr or std::shared_ptr (only if c++11). 正如Forever所提到的,最好的办法是开始使用智能指针, std::unique_ptrstd::shared_ptr (仅限c ++ 11)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM