 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ron Natalie Guest
|
Posted: Thu Jun 26, 2003 8:04 pm Post subject: Re: finding out if pointer to memory is valid |
|
|
"Schelli" <schellixx (AT) gmx (DOT) net> wrote
| Quote: |
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 no way to determine if a pointer points to something valid
or not. You'll need to keep track of it seperately (either whether it
is deleted, or have reference count so that the object is not deleted
while people are still using it).
|
|
| Back to top |
|
 |
Sin Guest
|
Posted: Thu Jun 26, 2003 8:25 pm Post subject: Re: finding out if pointer to memory is valid |
|
|
| Quote: | 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.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Jun 26, 2003 9:56 pm Post subject: Re: finding out if pointer to memory is valid |
|
|
"Sin" <broa29 (AT) hotmail (DOT) com> wrote
| Quote: | There is an undocumented call in ntdll.dll called RtlValidateHeap which will
do this on Windows NT/2000/XP...
|
Even with that call, you don't know if the object has been deleted. It may still
be allocated or otherwise valid as far as the heap is concerned, but contain
garbage as far as the program is concerned.
|
|
| Back to top |
|
 |
Alexander Terekhov Guest
|
Posted: Fri Jun 27, 2003 11:48 am Post subject: Re: finding out if pointer to memory is valid |
|
|
Schelli wrote:
| Quote: |
ok, thx for those posts, i was not sure if there is a way to know it,
because as i said, codeguard knows, but i think he tracks memory and
stuff in some ways i cant. the reason i am asking is, that it would
maybe more elegant to code it like this, if it is possible. but since
its not i am using the old methods i used before.
I already coded the interface a way, that it got pointers to all
structures that point to it, so if i destroy the structure it will use
that list first and set all pointers to itself to NULL before it's
destroying itself.
|
http://www.boost.org/libs/smart_ptr/weak_ptr.htm
regards,
alexander.
|
|
| Back to top |
|
 |
Dhruv Guest
|
Posted: Fri Jun 27, 2003 1:29 pm Post subject: Re: finding out if pointer to memory is valid |
|
|
On Thu, 26 Jun 2003 12:13:52 -0700, Schelli wrote:
| Quote: | 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.
|
You could use a reference to a pointer, so that if you set any one to NULL,
all of them become NULL. I don't know if this will fit in with your design
though.
Regards,
-Dhruv.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Jun 27, 2003 3:36 pm Post subject: Re: finding out if pointer to memory is valid |
|
|
"Schelli" <schellixx (AT) gmx (DOT) net> wrote
| Quote: | ok, thx for those posts, i was not sure if there is a way to know it,
because as i said, codeguard knows,
|
These sort of tools tend to scramble the code in ways outside the standard
in order to check for pointer miscues. They also tend to introduce a lot of
overhead in the process.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|