Re: finding out if pointer to memory is valid

Discussion in 'C++' started by Sin, Jun 26, 2003.

  1. Sin

    Sin Guest

    > 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.
     
    Sin, Jun 26, 2003
    #1
    1. Advertising

  2. Sin

    Ron Natalie Guest

    "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.
     
    Ron Natalie, Jun 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Natalie
    Replies:
    0
    Views:
    741
    Ron Natalie
    Jun 26, 2003
  2. Alexander Terekhov

    Re: finding out if pointer to memory is valid

    Alexander Terekhov, Jun 27, 2003, in forum: C++
    Replies:
    0
    Views:
    753
    Alexander Terekhov
    Jun 27, 2003
  3. Dhruv
    Replies:
    0
    Views:
    698
    Dhruv
    Jun 27, 2003
  4. Ron Natalie
    Replies:
    0
    Views:
    771
    Ron Natalie
    Jun 27, 2003
  5. sfs
    Replies:
    3
    Views:
    300
Loading...

Share This Page