This code is causing a memory leak for me, and I'm not sure why.
这段代码导致我的内存泄漏,我不知道为什么。
[EDIT] Included code from here into question:
[编辑]包含此处的代码:
#include "src/base.cpp"
typedef std::map<std::string, AlObj*, std::less<std::string>,
gc_allocator<std::pair<const std::string, AlObj*> > > KWARG_TYPE;
AlInt::AlInt(int val) {
this->value = val;
this->setup();
}
// attrs is of type KWARG_TYPE
void AlInt::setup() {
this->attrs["__add__"] = new AddInts();
this->attrs["__sub__"] = new SubtractInts();
this->attrs["__mul__"] = new MultiplyInts();
this->attrs["__div__"] = new DivideInts();
this->attrs["__pow__"] = new PowerInts();
this->attrs["__str__"] = new PrintInt();
}
int main() {
while (true) {
AlObj* a = new AlInt(3);
}
}
AlInt inherits from AlObj, which in turn inherits from gc. When I comment out the contents of setup() then I don't have a memory leak, this leads me to believe the issue is with the map not cleaning up, however I'm using the gc allocator, so I'm not sure where to look next. Thoughts?
AlInt继承自AlObj,后者继承自gc。当我注释掉setup()的内容然后我没有内存泄漏,这让我相信问题是地图没有清理,但是我使用的是gc分配器,所以我不确定在哪里看下一个。思考?
3 个解决方案
#1
4
The 'gc allocator' is allocating and looking after objects of this type:
'gc allocator'正在分配和处理这种类型的对象:
std::pair<const std::string, AlObj*>
Just because this object has a pointer in it does not mean it the allocator will call delete on it.
只是因为这个对象中有一个指针并不意味着分配器会在其上调用delete。
If you want the object created in setUp() to be GC then you need to allocate them via the GC. Or learn to use boost:ptr_map or shared_ptr.
如果您希望在setUp()中创建的对象是GC,那么您需要通过GC分配它们。或者学习使用boost:ptr_map或shared_ptr。
A map destroys (not deletes) the object it owns. In this case it owns the pointer not what the pointer points at. So when the map is destroyed it deallocates everything associated with the map and the object it owns (for pointers this means it does nothing).
地图会破坏(不删除)它拥有的对象。在这种情况下,它拥有的指针不是指针指向的指针。因此,当地图被破坏时,它会解除分配与地图及其拥有的对象相关的所有内容(对于指针,这意味着它什么都不做)。
If you have a map (or other container) that contains pointers. You must manually delete the pointers otherwise there will be a memory leak. Alternatively you can use boost::ptr_map or a map that contains a share_ptr
如果你有一个包含指针的地图(或其他容器)。您必须手动删除指针,否则会出现内存泄漏。或者,您可以使用boost :: ptr_map或包含share_ptr的映射
#2
0
Essentially what I'm saying is, the AlObj seems to be destructed, but not it's members(since it doesn't leak unless I put stuff in attrs).
基本上我所说的是,AlObj似乎被破坏了,但不是它的成员(因为它不会泄漏,除非我把东西放在attrs中)。
#3
0
The allocator is deleting your pairs. But deleting a pair doesn't delete members of the pair that happen to be pointers.
分配器正在删除您的对。但删除一对不会删除恰好是指针对的成员。
#1
4
The 'gc allocator' is allocating and looking after objects of this type:
'gc allocator'正在分配和处理这种类型的对象:
std::pair<const std::string, AlObj*>
Just because this object has a pointer in it does not mean it the allocator will call delete on it.
只是因为这个对象中有一个指针并不意味着分配器会在其上调用delete。
If you want the object created in setUp() to be GC then you need to allocate them via the GC. Or learn to use boost:ptr_map or shared_ptr.
如果您希望在setUp()中创建的对象是GC,那么您需要通过GC分配它们。或者学习使用boost:ptr_map或shared_ptr。
A map destroys (not deletes) the object it owns. In this case it owns the pointer not what the pointer points at. So when the map is destroyed it deallocates everything associated with the map and the object it owns (for pointers this means it does nothing).
地图会破坏(不删除)它拥有的对象。在这种情况下,它拥有的指针不是指针指向的指针。因此,当地图被破坏时,它会解除分配与地图及其拥有的对象相关的所有内容(对于指针,这意味着它什么都不做)。
If you have a map (or other container) that contains pointers. You must manually delete the pointers otherwise there will be a memory leak. Alternatively you can use boost::ptr_map or a map that contains a share_ptr
如果你有一个包含指针的地图(或其他容器)。您必须手动删除指针,否则会出现内存泄漏。或者,您可以使用boost :: ptr_map或包含share_ptr的映射
#2
0
Essentially what I'm saying is, the AlObj seems to be destructed, but not it's members(since it doesn't leak unless I put stuff in attrs).
基本上我所说的是,AlObj似乎被破坏了,但不是它的成员(因为它不会泄漏,除非我把东西放在attrs中)。
#3
0
The allocator is deleting your pairs. But deleting a pair doesn't delete members of the pair that happen to be pointers.
分配器正在删除您的对。但删除一对不会删除恰好是指针对的成员。