gishara
gishara

Reputation: 845

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. 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.

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++.

Thanks a lot.

Upvotes: 2

Views: 912

Answers (2)

Thierry Franzetti
Thierry Franzetti

Reputation: 1863

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.

You can do this in 2 ways :

  • either using gflags contained in the 'Debugging Tools for Windows'. 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'
  • 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). 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.

Upvotes: 1

Alexander Gessler
Alexander Gessler

Reputation: 46607

You are presumably corrupting the memory used by the vector somewhere else.

Upvotes: 5

Related Questions