Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: finding out if pointer to memory is valid

Reply
Thread Tools

Re: finding out if pointer to memory is valid

 
 
Sin
Guest
Posts: n/a
 
      06-26-2003
> 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.


 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      06-26-2003

"Sin" <> wrote in message news:0_IKa.4604$.. .

> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer Shivanand Kadwadkar C Programming 83 01-08-2011 08:18 AM
Re: finding out if pointer to memory is valid Ron Natalie C++ 0 06-27-2003 03:36 PM
Re: finding out if pointer to memory is valid Dhruv C++ 0 06-27-2003 01:29 PM
Re: finding out if pointer to memory is valid Alexander Terekhov C++ 0 06-27-2003 11:48 AM
Re: finding out if pointer to memory is valid Ron Natalie C++ 0 06-26-2003 08:04 PM



Advertisments