the memory leak
-
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
-
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
valerie99 wrote:
strcpy( FileName, BinPath );
Where is Filename allocated ? Is it ever deleted ? C style string handling is a messy pile of crap, and the perfect way to get memory leaks. Why are you not using C++ here ? Christian Graus - Microsoft MVP - C++
-
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
-
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
i don't see any here... you
CallSumBin = new CALLSUM_BIN( FileName );
and alwaysdelete CallSumBin;
it then... so, you might have a look at the CALLSUM_BIN constructor... does it allocate some memory that is not deleted (at the end of the constructor or in the destructor ?
TOXCCT >>> GEII power
[toxcct][VisualCalc] -- modified at 3:18 Thursday 3rd November, 2005 -
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
Is your code perhaps calling the function containing
CallSumBin = new CALLSUM_BIN( FileName );
more than once without having called the closedown() function? If so, that will cause a memory leak. I would suggest the following:if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); **if (CallSumBin != NULL) { delete CallSumBin; CallSumBin = NULL; }** CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; }
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193 -
good afternoon, could anyone see where is the leak from? thank you! I've checked the momory allocation, the leak is happening around here: strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); if ( _access ( FileName, 0 ) != 0 ) { sprintf( ErrorText, "File does not exist -> %s", FileName ); Message( ErrorText, VD_ERROR ); return 0; } m_information_line = "Loading CALLSUM Binary File."; PostUpdate(); CallSumBin = new CALLSUM_BIN( FileName ); if ( !CallSumBin ) { Message( "Unable to allocate CallSumBin binfile.", VD_ERROR ); return 0; } here is the delete in closedown() function: if ( CallSumBin ) { if ( !IsStop() ) { strcpy( FileName, BinPath ); strcat( FileName, "CALLSM" ); strcat( FileName, Cycle); strcat( FileName, ".BIN" ); m_information_line = "Saving updated CALLSM binary file"; PostUpdate(); CallSumBin->Save( FileName ); fprintf( Report,"Updated CALLSM binary file saved.\r\n" ); } delete CallSumBin; } m_information_line = " "; PostUpdate(); -- modified at 17:23 Wednesday 2nd November, 2005
As krmed already stated, make sure your allocation routine isn't being called multiple times. However, I wouldn't make krmed's change, since that might just be fixing the symptoms and not the real issue. It is the memory allocation version of the "off by one" error where someone fixes the problem by just adding one without tracking down the reason why the value is off by one. Another problem might be that your closedown code isn't being invoked. I often see people spend endless amounts of time trying to figure out why are routine isn't working when in fact, the routine isn't even being invoked. Both of those problems are the same class of problem, improper execution path. Of course, this is all assuming that it is CallSumBin that is leaking. If you haven't verified that, then you might be looking at the wrong issue. As to what Christian said, he is right. Constructing the filename like that is akin to running with scissors. As long as you are careful, things are "ok". But a slight mistake can cause memory corruption (which can manifest itself as a leak in some instances) and security issues. I won't go so far as saying C style is crap, mainly because I have seen far too many string processing systems created with managed strings that are just dogs. But you are playing with fire and really need to understand what you are doing when you do work with C strings. Tim Smith I'm going to patent thought. I have yet to see any prior art. -- modified at 9:56 Thursday 3rd November, 2005