In this section:
This error is generated whenever a parameter declared as an array is actually passed a pointer to a block of memory that has been freed.
Array parameter is a dangling pointer
The following code frees its memory block before passing it to
Diagnosis at Runtime
- Line 2: Source file and line at which the problem was detected.
- Line 3: Description of the problem and the name of the parameter that is in error.
- Line 4: Value of the pointer that was passed and has been deallocated.
- Line 5: Information about the block of memory addressed by this pointer, including information about where this block was allocated.
- Line 10: Indication of the line at which this block was freed.
- Line 13: Stack trace showing the function call sequence leading to the error.
This error is normally caused by freeing a piece of memory too soon. A good strategy is to examine the line of code indicated by the diagnostic message which shows where the memory block was freed and check that it should indeed have been de-allocated.
A second check is to verify that the correct parameter was passed to the subroutine.
A third strategy which is sometimes useful is to NULL pointers that have been freed and then check in the called subroutine for this case. Code similar to the following is often useful:
The combination of resetting the pointer to
NULL after freeing it and the check in the called subroutine prevents misuse of dangling pointers.