In this section:
Overview
This error is generated whenever an attempt is made to copy a pointer to a block of memory which has been freed. This error is suppressed by default. Attempt to copy dangling pointerCode Description Enabled Reported Platform COPY_DANGLING Runtime Windows/Unix
Problem
In the following code, the a
pointer in line 7 is freed by calling delete[]
. The next line attempts to copy from the address a
into the variable b
. Because a
has already been freed, b
will not point to valid memory, either.
* * File: copydang.cpp */ int main() { char *a = new char [10], *b; delete[] a; b = a; return 0; }
Diagnosis at Runtime
[copydang.cpp:8] **COPY_DANGLING** >> b = a; Copying dangling pointer: a Pointer : 0x0007b6a0 In block : 0x0007ebc0 thru 0x0007ebc9 (10 bytes) a, allocated at: main() copydang.cpp, 5 stack trace where memory was freed: main() copydang.cpp, 7 Stack trace where the error occurred: main() copydang.cpp, 8
- Line 1: Source line at which the problem was detected.
- Line 3: Brief description of the problem.
- Line 5: Description of the pointer that is dangling.
- Line 9: Stack trace showing the function call sequence leading to the error.
- Line 11: Stack trace showing the function call sequence leading the error.
Repair
The simple way to avoid this problem is to not attempt to reuse pointers after they have been freed. Check that the deallocation that occurs at the indicated location should have taken place. Also check if pointer you are (mis)using should be pointing to a block allocated at the indicated place.