Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I have written a small program where in I have created a vector of string. Then I created a dynamic array of pointers for characters and assigned the vectors to the dynamic array.

But it is giving memory leaks.So please help me to deallocate the memory.

the code is :

C++
int main
{
     _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

     vector<string> vStringList;

     for(int i = 0;i <5;i++)
     {
         char filename[1024];
         sprintf(filename,"%s_%d","myfile",i+1);
         string str;
         str.assign(filename);
         vStringList.push_back(str);
     }

     char **pStringArray = new char*[vStringList.size()+1];

     for(i = 0; i< vStringList.size();i++)
     {
         char curFileName[1024];
         pStringArray[i] = const_cast<char*>(vStringList[i].c_str());
     }
     delete []pStringArray ;
    _CrtDumpMemoryLeaks();
    return 0;
 }
Posted
Updated 22-Aug-12 23:56pm
v3
Comments
Maximilien 23-Aug-12 6:26am    
what is the use of pStringArray ? what are you trying to do ?
Sergey Chepurin 25-Aug-12 11:04am    
Do not guess about memory leaks, use professional tools like these (free) ones -Valgrind (*nix) or Visual leak detector (Windows).

At the time of your call of CrtDumpMemoryLeaks your vector<string> is still alive and has still memory on the heap allocated. Try this:

     _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
 
     {
          vector<string> vStringList;
          ...
          delete []pStringArray ;
     }
    _CrtDumpMemoryLeaks();

</string>


In this way vStringList will go out of scope and release its memory.
 
Share this answer
 
Comments
JackDingler 23-Aug-12 11:17am    
Good example.
pasztorpisti 24-Aug-12 19:19pm    
5ed
nv3 25-Aug-12 3:44am    
Thanks!
There is no need to need to delete the contents of the vStringList. That memory will be auto deleted when it goes out of scope.

The reason you are detecting memory leaks, is because you're checking for leaks before the function ends and the objects go out of scope. The objects haven't been cleaned up yet (by design), so you're seeing objects that are legitimately allocated and mistaking them for leaks.

An easier and more reliable way to check for leaks in Microsoft C++ code is to add the following code snippet to the top of your .cpp modules, after the header includes.

C++
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


Then run the program in the debugger. When the program exits, you'll see any leaks in the trace window.

This method will be more reliable for you, because the leak checking is done after all of the globals that you've instantiated, go out of scope.



C++
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


int main()
{
     vector<string> vStringList; // <-- This will not leak

     char * pLeak = new char[1024]; // <-- This will leak

     for(int i = 0;i <5;i++)
     {
         char filename[1024];
         sprintf(filename, "%s_%d", "myfile",i+1);

         vStringList.push_back(filename);
     }

    return 0;
 }
 
Share this answer
 
v2
Comments
nv3 23-Aug-12 10:01am    
Good comments, Jack.
I really don't see what you're trying to do there or better why you're doing it.
Are you trying to clear the memory that is allocated by the <string> Vector, by using the char pointers you get from c_str() of the single vector elements?

In that case this is some strange way of thinking. In fact completely releasing the memory occupied by a container like vector isn't that straightforward as one would think. Because decreasing the size of a vector e.g. with pop() or erase() doesn't actually free the memory (decrease the capacity) of the vector.

To really shrink and erase elements you can make use of the shrink-to-fit idiom which is generally defined as:
container<T>().swap(c)
with c beeing of type container.

But first of all you should answer this question:
What are you trying to achieve?
 
Share this answer
 
v4
I think you need to erase()[^] the elements of your vector before you delete it.
 
Share this answer
 
Unlike your leak detection setup your deallocation is fine. If you use the _CRTDBG_LEAK_CHECK_DF flag then you don't have to dump the leaks with _CrtDumpMemoryLeaks() because the CRT will do that for you after you return from your main()! If you dont call _CrtDumpMemoryLeaks() directly then your std::vector goes out of scope when you return from main() so it doesnt appear as a leak! The _CrtDumpMemoryLeaks() call is only for extreme cases when you want to dump the currently allocated memory chunks in the middle of the execution time of your program. If all you want is dumping leaks on exit you need only the _CRTDBG_LEAK_CHECK_DF flag.

Another good advice: Never put much into the main() of your C/C++ program. You can write your program for example like this:
C++
class MyProg
{
public:
    int Main()
    {
        vector<string> vStringList;
     
        for(int i = 0;i <5;i++)
        {
            char filename[1024];
            sprintf(filename,"%s_%d","myfile",i+1);
            string str;
            str.assign(filename);
            vStringList.push_back(str);
        }
     
        char **pStringArray = new char*[vStringList.size()+1];
     
        for(i = 0; i< vStringList.size();i++)
        {
            char curFileName[1024];
            pStringArray[i] = const_cast<char*>(vStringList[i].c_str());
        }
        delete []pStringArray ;
        return 0;
    }
}

int main()
{
    _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    MyProg prog;
    return prog.Main();
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900