简体   繁体   English

释放std :: vector C ++的内存

[英]Free the memory of a std::vector C++

I have a vector as below. 我有一个向量如下。

std::vector<std::string> exportNameList;

I am adding elements to this by using push_back method. 我通过使用push_back方法向其中添加元素。 But I am getting a debug assertion as " 但是我得到一个调试断言为“

"Windows has triggered a breakpoint in AxCent.exe.This may be due to a corruption of the heap, which indicates a bug in AxCent.exe or any of the DLLs it has loaded. “ Windows在AxCent.exe中触发了一个断点。这可能是由于堆损坏所致,这表明AxCent.exe或其已加载的任何DLL中存在错误。

This happens when it calls the destructor of the class. 当它调用类的析构函数时会发生这种情况。 When I refer to the call stack I was directed to the following code block in the vector class. 当我引用调用堆栈时,我被定向到向量类中的以下代码块。

~vector()
{   // destroy the object
    _Tidy();
}

What I noticed is there is an error when deleting the vector. 我注意到删除向量时出现错误。 Am I correct? 我对么? How do I fix this? 我该如何解决? I have referred to many examples, but did not manage to work this out yet. 我已经提到了许多示例,但是尚未设法解决。 I am quite new to C++. 我对C ++很陌生。

Thanks a lot. 非常感谢。

您可能正在破坏向量在其他地方使用的内存。

The bug you encounter may not be directly related to your vector. 您遇到的错误可能与您的媒介没有直接关系。 If the memory (heap) is corrupted before the destructor of your vector is called, then the heap manager may only detect the corruption at this time (freeing the structure dynamically allocated by the vector or the dynamically allocated strings inside). 如果在调用向量的析构函数之前内存(堆)已损坏,则堆管理器此时可能仅检测到破坏(释放由向量动态分配的结构或内部动态分配的字符串)。

In my opinion, the best way to handle these kinds of bugs on the Windows platform is to activate the Full Page Heap for your program. 我认为,在Windows平台上处理此类错误的最佳方法是为程序激活“全页堆”。

You can do this in 2 ways : 您可以通过2种方式执行此操作:

  • either using gflags contained in the 'Debugging Tools for Windows'. 使用“ Windows调试工具”中包含的gflag。 Run it as administrator, go to the 'Image File' tab, enter the name of your EXE in the Image field (AxCent.exe), press TAB and check 'Enable page heap' and press 'Apply' 以管理员身份运行它,转到“图像文件”选项卡,在“图像”字段(AxCent.exe)中输入EXE的名称,按TAB并选中“启用页面堆”,然后按“应用”
  • or use Application Verifier. 或使用应用程序验证程序。 Select your executable through the File/App application menu make sur the check 'Basics/Heap' is checked and click save. 通过“文件/应用程序”菜单选择可执行文件,然后选中“基本/堆”,然后单击“保存”。

This setting will be applied whenever this application is launched. 每当启动此应用程序时,将应用此设置。

Then run your application under a debugger (WindDbg or Visual Studio). 然后在调试器(WindDbg或Visual Studio)下运行您的应用程序。 If the memory is corrupted before your vector deletion, the debugger should break at this point. 如果在删除矢量之前内存已损坏,则调试器应在此时中断。

When you are finished tracking the bug, don't forget to turn Full Page Heap off. 当您完成对bug的跟踪时,请不要忘记关闭“整页堆”。

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

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