- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
*** 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.
コピーされたリンク
14 返答(返信)
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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 );
}
...
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
[ 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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
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
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
>>...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.
- 新着としてマーク
- ブックマーク
- 購読
- ミュート
- RSS フィードを購読する
- ハイライト
- 印刷
- 不適切なコンテンツを報告
>>...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.