> ok, i got 2 pointers pointing to same memory/object. with one of the
> pointers i delete the object and i could set this pointer to NULL to
> know that it was deleted, but the other pointer still remains pointed
> there and does not "know" the memory was deleted. so if i now access
> this memory i might get a crash !
>
> now if i switch codeguard (BC++ 6.0) on, he immediately says accessing
> non reserved memory, or something like this, although the program does
> not crash yet, well, sometimes it does, but thats by chance of course.
>
> can i somehow find out that the memory this pointer is pointing to was
> deleted ? or do i just have to find a way to set the other pointer to
> NULL so i know.
There is an undocumented call in ntdll.dll called RtlValidateHeap which will
do this on Windows NT/2000/XP... But in my opinion using it would be wrong
and your design needs to be reviewed. If the variable is accessible globally
you can set it to NULL, which will avoid the problem. If not then you really
need to set the responsabilities straight in your design.
One way would be to handle the creation/release of this object from a
central point, a factory if you will. This factory would keep a reference
count and delete the object when it falls to zero... It could also release
it immediatly, and simply do nothing if other pieces of code release it. The
first method is safer, since it ensures your object is loaded until it's
released.
Alex.
|