C ++中带有boost :: ptr_vector的内存泄漏 [英] Leaking Memory In C++ With boost::ptr_vector
问题描述
我一直在从事一个项目,但是最近我检查程序是否泄漏,结果是泄漏很多.
I been working in a project, but recently I check if my program has some leaking and the results is that it's leaking and a lot.
我使用_CrtDumpMemoryLeaks();
接收所有泄漏消息,并且检查它们中的大多数与boost有关,我知道这一定是我的问题,但我不明白为什么泄漏消息.
I use _CrtDumpMemoryLeaks();
to receive all the messages of leaking and I check that most of them are related with boost, I know that it must be my problem, but I can't understand why it's leaking.
在调试输出中,向我显示了以下几行:
In the debug output shows me these lines:
Dumping objects ->
{673} normal block at 0x00E075E0, 8 bytes long.
Data: <H @e > 48 92 E0 00 40 65 E0 00
{671} normal block at 0x00E065C0, 8 bytes long.
Data: <@e > 40 65 E0 00 00 00 00 00
{669} normal block at 0x00E06540, 68 bytes long.
Data: < e mountains.pn> C0 65 E0 00 6D 6F 75 6E 74 61 69 6E 73 2E 70 6E
{665} normal block at 0x00E063B0, 8 bytes long.
Data: <H > 48 92 E0 00 00 00 00 00
{663} normal block at 0x00E09248, 68 bytes long.
Data: < c nubes.png > B0 63 E0 00 6E 75 62 65 73 2E 70 6E 67 00 CD CD
这使我相信问题出在我使用这些字符串的地方,而这些字符串的第一个调用是在这些行中:
Which leads me to believe that the problem is where I use those strings, and the first call with those are in these lines:
tutorialLevel->addLayerToList("nubes.png", 1600.f, 720.f, 1.0f, 0.0f, 0.1f, true);
tutorialLevel->addLayerToList("mountains.png", 1600.f, 720.f, speedXVectorPanda.at(0), 0.0f, 0.5f, false);
然后是实际功能addLayerToList
:
void Level::addLayerToList(std::string name, GLfloat widthLayer, GLfloat heightLayer, GLfloat velX, GLfloat velY,
GLfloat constantX, bool hasRepetition)
{
layersList.push_back( new Layer(name, widthLayer, heightLayer, velX, velY, constantX, hasRepetition) );
}
layersList的定义如下:
And layersList is define like this:
boost::ptr_vector< Layer > layersList;
也许,我误解了Boost中指针的所有权是如何工作的,但是在我最近检查的示例中,这是将对象所有权传递给ptr_vector
的正确方法,对吗?
Maybe, I misunderstood how the ownership of pointers work in Boost, but in the examples I recently check, this is a correct way to pass ownership of the object to the ptr_vector
, am I wrong?
我的另一个问题是,是否有必要释放向量的指针,还是最好让auto_ptr
做他的工作?
And my other question is, if it's necessary release the pointers for the vector, or it's better leave the auto_ptr
do his work?
感谢您的帮助.
推荐答案
取决于放置位置,但在几乎所有情况下,_CrtDumpMemoryLeaks
不会不向您显示使用STL/BOOST时的真相智能指针.它将STL中new
的用法视为内存泄漏.
Depending on where you placed it but in almost all cases, _CrtDumpMemoryLeaks
will not show you the truth when using STL/BOOST smart pointers. It will see the usage of new
within the STL as a memory leak.
这篇关于C ++中带有boost :: ptr_vector的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!