Insure++ Reference - LEAK_ASSIGN
This error is generated whenever a pointer assignment occurs which will
prevent a block of dynamically allocated memory from ever being freed.
Normally this happens because the pointer being changed is the only one
that still points to the dynamically allocated block.
This code allocates a block of memory, but then reassigns the pointer
to the block to a static memory block. As a result, the dynamically
allocated block can no longer be freed.
1: /*
2: * File: leakasgn.c
3: */
4: #include <stdlib.h>
5:
6: main()
7: {
8: char *b, a[10];
9:
10: b = (char *)malloc(10);
11: b = a;
12: return (0);
13: }
[leakasgn.c:11] **LEAK_ASSIGN**
1. >> b = a;
2. Memory leaked due to pointer reassignment: <return>
3. Lost block: 0x000173e8 thru 0x000173f1 (10 bytes)
block allocated at:
malloc() (interface)
main() leakasgn.c, 10
Stack trace where the error occurred:
4. main() leakasgn.c, 11
- Source line at which the problem was detected.
- Description of the problem and the expression that is in error.
- Description of the block of memory that is about to be lost,
including its size and the line number at which it was allocated.
- Stack trace showing the function call sequence leading to the error.
In many cases, this problem is caused by simply forgetting to free a
previously allocated block of memory when a pointer is reassigned. For
example, the leak in the example code can be corrected as follows
10: b = (char *)malloc(10);
11: free(b);
12: b = a;
Some applications may be unable to free memory blocks and may not
need to worry about their permanent loss. To suppress these error
messages, add the option
insure++.suppress LEAK_ASSIGN
to the .psrc file.
INSURE_WARNING
LEAK_FREE
|