Software Archive
Read-only legacy content
17061 Discussions

Memory Leaks Detection and Tracing in software

SergeyKostrov
Valued Contributor II
451 Views
*** Memory Leaks Detection and Tracing in software *** [ Abstract ] Memory Leaks is a result of incorrect processing of dynamically allocated memory with CRT-functions like malloc, calloc, free, or with C++ operators like new, delete. Memory Leaks happen when a dynamically allocated memory is Not released, or when a dynamically allocated memory is Not released before the pointer to that memory is reused again to allocate another block of memory. It is important to understand that this is Not a "negative" feature of C or C++ programming languages and memory leaks could be classified as a logical error in C or C++ source codes. A Software Developer is completely responsible for all cases when memory leaks are "created" in software.
0 Kudos
14 Replies
SergeyKostrov
Valued Contributor II
451 Views
[ C++ compilers used for Memory Leaks Detection and Tracing evaluations ] Borland C++ compiler v5.5.1 32-bit MinGW C++ compiler v5.1.0 32-bit Microsoft C++ compiler ( VS2005 PE ) 32-bit Intel C++ compiler v12.1.7 ( u371 ) 32-bit Watcom C++ compiler v2.0.0 32-bit
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Memory Leaks examples in C source codes ]

 Example 1

 A simple C code that demonstrates a couple of logical errors and a memory leaks of
 128 bytes on a 32-bit platform:

 ...
 void main( void )
 {
  int *pArray = ( int * )malloc( 32 * sizeof( int ) );

  for( int i = 0; i < 32; i += 1 )
   pArray = i;

  //...
  //...Some Processing...
  //...

  exit( 0 );
 }
 ...

 Note: Memory is dynamically allocated but never released.

 

0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
Example 2

 ...
 void main( void )
 {
  int *pArray = ( int * )malloc( 32 * sizeof( int ) );

  for( int i = 0; i < 32; i += 1 )
   pArray = i;

  //...
  //...Some Processing A...
  //...

  pArray = ( int * )malloc( 32 * sizeof( int ) );

  for( int i = 0; i < 32; i += 1 )
   pArray = i + 1;

  //...
  //...Some Processing B...
  //...

  exit( 0 );
 }
 ...

 Note: Memory is dynamically allocated twice using the same pointer pArray,
 is Not released before reusing the pointer and before termination of the application.

 

0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
Example 3

 Here is the same C code that demonstrates correct processing with a pointer pArray and
 without memory leaks:

 ...
 void main( void )
 {
  int *pArray = NULL;

  pArray = ( int * )malloc( 32 * sizeof( int ) );

  if( pArray != NULL )
  {
   for( int i = 0; i < 32; i += 1 )
    pArray = i;
  }

  //...
  //...Some Processing A...
  //...

  if( pArray != NULL )
   free( pArray );

  pArray = ( int * )malloc( 32 * sizeof( int ) );

  if( pArray != NULL )
  {
   for( int i = 0; i < 32; i += 1 )
    pArray = i + 1;
  }

  //...
  //...Some Processing B...
  //...

  if( pArray != NULL )
   free( pArray );

  exit( 0 );
 }
 ...

 

0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Borland C++ compiler v5.5.1 32-bit ] Allocating Memory Tracer Data Table Application - BccTestApp - WIN32_BCC ( 32-bit ) - Debug Tests: Start > Test0001 Start < ********************************************** Configuration - WIN32_BCC ( 32-bit ) - Debug CTestSet::InitTestEnv - Passed * CMemorySet Start * > CMemorySet External Functions < Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected InitMemoryTracer - Test for 16384 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table InitMemoryTracer - Test for 16777216 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table Deallocating Memory Tracer Data Table Completed InitMemoryTracer - Passed ReleaseMemoryTracer - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegister - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegisterEx - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtFreeUnregister - Passed CrtFreeUnregisterEx - Passed MemoryTracerData - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected MemoryTracerIntegrity - Passed * CMemorySet End * Test Completed in 135359 ticks > Test0001 End < Tests: Completed Attention: Memory Leaks of 777 bytes created
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ MinGW C++ compiler v5.1.0 32-bit ] Allocating Memory Tracer Data Table Application - MgwTestApp - WIN32_MGW ( 32-bit ) - Debug Tests: Start > Test0001 Start < ********************************************** Configuration - WIN32_MGW ( 32-bit ) - Debug CTestSet::InitTestEnv - Passed * CMemorySet Start * > CMemorySet External Functions < Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected InitMemoryTracer - Test for 16384 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table InitMemoryTracer - Test for 16777216 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table Deallocating Memory Tracer Data Table Completed InitMemoryTracer - Passed ReleaseMemoryTracer - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegister - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegisterEx - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtFreeUnregister - Passed CrtFreeUnregisterEx - Passed MemoryTracerData - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected MemoryTracerIntegrity - Passed * CMemorySet End * Test Completed in 245234 ticks > Test0001 End < Tests: Completed Attention: Memory Leaks of 777 bytes created
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Microsoft C++ compiler ( VS2005 PE ) 32-bit ] Allocating Memory Tracer Data Table Application - ScaLibTestApp - WIN32_MSC ( 32-bit ) - Debug Tests: Start > Test0001 Start < ********************************************** Configuration - WIN32_MSC ( 32-bit ) - Debug CTestSet::InitTestEnv - Passed * CMemorySet Start * > CMemorySet External Functions < Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected InitMemoryTracer - Test for 16384 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table InitMemoryTracer - Test for 16777216 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table Deallocating Memory Tracer Data Table Completed InitMemoryTracer - Passed ReleaseMemoryTracer - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegister - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegisterEx - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtFreeUnregister - Passed CrtFreeUnregisterEx - Passed MemoryTracerData - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected MemoryTracerIntegrity - Passed * CMemorySet End * Test Completed in 182578 ticks > Test0001 End < Tests: Completed Attention: Memory Leaks of 777 bytes created
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Intel C++ compiler v12.1.7 ( u371 ) 32-bit ] Allocating Memory Tracer Data Table Application - IccTestApp - WIN32_ICC ( 32-bit ) - Debug Tests: Start > Test0001 Start < ********************************************** Configuration - WIN32_ICC ( 32-bit ) - Debug CTestSet::InitTestEnv - Passed * CMemorySet Start * > CMemorySet External Functions < Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected InitMemoryTracer - Test for 16384 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table InitMemoryTracer - Test for 16777216 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table Deallocating Memory Tracer Data Table Completed InitMemoryTracer - Passed ReleaseMemoryTracer - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegister - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegisterEx - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtFreeUnregister - Passed CrtFreeUnregisterEx - Passed MemoryTracerData - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected MemoryTracerIntegrity - Passed * CMemorySet End * Test Completed in 1342344 ticks > Test0001 End < Tests: Completed Attention: Memory Leaks of 777 bytes created
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Watcom C++ compiler v2.0.0 32-bit ] Allocating Memory Tracer Data Table Application - WccTestApp - WIN32_WCC ( 32-bit ) - Debug Tests: Start > Test0001 Start < ********************************************** Configuration - WIN32_WCC ( 32-bit ) - Debug CTestSet::InitTestEnv - Passed * CMemorySet Start * > CMemorySet External Functions < Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected InitMemoryTracer - Test for 16384 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table InitMemoryTracer - Test for 16777216 Memory Blocks Deallocating Memory Tracer Data Table Completed Allocating Memory Tracer Data Table Deallocating Memory Tracer Data Table Completed InitMemoryTracer - Passed ReleaseMemoryTracer - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegister - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtMallocRegisterEx - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected CrtFreeUnregister - Passed CrtFreeUnregisterEx - Passed MemoryTracerData - Passed Memory Blocks Allocated : 0 Memory Blocks Released : 0 Memory Blocks NOT Released: 0 Memory Tracer Integrity Verified - Memory Leaks NOT Detected MemoryTracerIntegrity - Passed * CMemorySet End * Test Completed in 590563 ticks > Test0001 End < Tests: Completed Attention: Memory Leaks of 777 bytes created
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
[ Performance Memory Leaks Detection and Tracing evaluations ]

 Borland C++ compiler v5.5.1 32-bit   - Test Completed in  135359 ticks
 Microsoft C++ compiler ( VS2005 PE ) 32-bit - Test Completed in  182578 ticks
 MinGW C++ compiler v5.1.0 32-bit  - Test Completed in  245234 ticks
 Watcom C++ compiler v2.0.0 32-bit  - Test Completed in  590563 ticks
 Intel C++ compiler v12.1.7 ( u371 ) 32-bit - Test Completed in 1342344 ticks

[ Summary ]

 - Borland C++ compiler v5.5.1 32-bit - fastest
 - Intel C++ compiler v12.1.7 ( u371 ) 32-bit - slowest

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
451 Views

Sergey,

In your first example #3:

You are writing beyond the end of the array allocated. This may (is likely to) corrupt the heap.

Upon exit of main, the process terminates, the dangling pointer to memory allocated but not returned, will get cleaned up and the system will not lose memory (though Valgrind will point this out to you).

In your second example #4, the second malloc will likely attempt to allocate from the corrupted heap (as corrupted by the first loop).

(similar issues in remaining examples)

Jim Dempsey

0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
>>...You are writing beyond the end of the array allocated. This may (is likely to) corrupt the heap. Thanks for the comment, Jim. It is good to know that some IDZ users really read posts and analyse source codes! The for-loop-with-777 actually came from another test case and I will correct that 777-error in examples.
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
>>...This may (is likely to) corrupt the heap... Memory corruption is another subject and let's not discuss it here. Examples 1, 2 and 3 ( pseudo-codes ) are for demonstration only.
0 Kudos
SergeyKostrov
Valued Contributor II
451 Views
Here is a file with source codes of the test.
0 Kudos
Reply