ptr_vector如何管理内存? [英] How does ptr_vector manage memory?

查看:79
本文介绍了ptr_vector如何管理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Opengl进行低级编码的C ++.我来自objc,所以我对内存管理有一些了解,但似乎无法了解提升"库如何管理诸如ptr_vector之类的容器类型.

I'm currently getting myself into c++ for lower level coding with opengl. I come from a heavy objc background so I have some understanding about memory management but I can't seem to get how the "boost" library manages container types like ptr_vector.

我认为我的问题与以下事实有关:我不知道ptr_vector如何管理自身及其对象的破坏.

I think my problem is related to the fact that I have no idea how ptr_vector manages the destruction of itself and its objects.

请查看以下代码:

// Header file
...
ptr_vector<IObject3D> objects;
...

// Implementation file
...
void ApplicationEngine::init()
{
    WavefrontObject3D *object = new WavefrontObject3D("Ninja.obj");
    objects.push_back(object); 
}
...

因此,对于实际问题:我是否通过"object"变量在此处创建泄漏?

So, for the actually question: am I creating a leak here through the "object" variable?

我习惯在objc中通过显式调用手动保留和释放对象: 以前,我不得不alloc init WavefrontObject3D object,将其添加到数组中,然后release将该对象添加到数组中,以免泄漏.

I'm used to retain and release my objects manually with explicit calls in objc: previously I had to alloc init the WavefrontObject3D object, add it to an array and afterwards release that same object to avoid leaks.

但是当我在push_back调用之后添加delete object时,调用了WavefrontObject3D object的解构函数.这给了我一个提示,即ptr_vector没有保留object变量.我的假设正确吗?

But when I add a delete object after the push_back call the deconstructor of the WavefrontObject3D object is called. This gives me a hint that the ptr_vector isn't retaining the object variable. Is my assumption correct?

其他但相关的问题:假设我要销毁包含类ApplicationEngine,而不必在ptr_vector或其管理的元素上调用某种解构函数?

Additional, but related, question: let's say I want to destroy the containing class ApplicationEngine don't I have to call some kind of deconstructor on the ptr_vector or the elements it manages?

推荐答案

否,这不会造成泄漏.当容器超出范围时,所有ptr_*容器都将删除存储在其中的对象.

No, this doesn't create a leak. All ptr_* containers will delete objects that are stored in them when the container goes out of scope.

如果在将对象添加到容器后删除该对象,则会创建undefined behavior,因为容器将尝试再次删除它.

If you delete the object after adding it to the container, you will create undefined behavior as the container will attempt to delete it again.

其他问题:不,如果按值存储ptr_vector,则其生存期由周围类的范围管理.

Additional question: No, if you store the ptr_vector by value its lifetime is managed by the scope of the surrounding class.

让我们写一个ptr_vector的简单实现.它不支持间接迭代器和自定义删除器以及许多其他功能,但是显示了所使用的原理.

Let's write a simple implementation of ptr_vector. It has no support for indirect iterators and custom deleters and a lot of other things but shows the principles used.

template <typename T>
class ptr_vector {
public:
  // assume control over it
  void push_back(T* x) 
  { if(x) c_.push_back(x); else throw bad_pointer(); }

  ~ptr_vector() { 
    // delete everything that is stored here
    for(auto x : c_)  delete x;
  }
private:
  std::vector<T*> c_;
};


// a user class
struct user_class {
  void addSomething() { x.push_back(new int(23)); }
  ptr_vector<int> x;
};

如果用户类超出范围,则ptr_vector的析构函数将 被调用,所有内存将被回收.看不到泄漏.

If user class goes out of scope, the destructor of ptr_vector will be called and all memory will be reclaimed. No leak in sight.

这篇关于ptr_vector如何管理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆