A pointer that was pointing to the right object, but the memory pointed by this pointer was then returned to the MemoryAllocationEngine?
, while some parts of the program still refer to it.
The memory is therefore reassigned to other parts of the program, which change it at will, so the pointer seems to dangle: The data pointed to by this dangling pointer is meaningless, and dereferencing it will normally cause UndefinedBehavior
This can't happen when using GarbageCollection
. It also can't happen when using arena-based memory allocation with safety enforced by a type system, such as the "region" mechanism in CycloneLanguage
In some systems, for example the ObjectCapabilityOperatingSystem
, deleting an arena ("zapping a space bank" in EROS/KeyKOS terminology), results in pointers that dangle, but that have well-defined error behaviour. So dereferencing a DanglingPointer
does not necessarily
There are other ways to create WildPointer
- Unchecked typecasts (converting between different pointer types; or between pointers and integers)
- Pointers (with unlimited extent) into TheStack
- Uninitialized pointers
If a language has none
of these (JavaLanguage
is an example), then dangling pointers aren't possible. Elimination of manual memory allocation is necessary
to eliminate dangling pointers; but not sufficient.
I think you meant manual memory deallocation. See also ResourceAcquisitionIsInitialization and BoostSharedPtr for how to eliminate dangling pointers in C++.