Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kranar

Ensuring that a pointer is valid.

This topic is 5229 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basically, in my game, coded in C++, I have objects that are created dynamically (using new). Various classes point to the same object using pointers, the thing is that some classes are responsible for deleting the objects once they''re destroyed, and the other classes won''t have any idea that they''re destroyed so when the pointer is dereferenced my program crashes. How can I check to see if the information that a pointer points to is valid or invalid? I suppose as a simple example to simplify what I mean...
int* x = new int(0);
int* y = x;
delete x;

// do some kind of check to see if y is valid

   ...
// if y is valid

*y += 5;

// if y is invalid

y = NULL;
Any ideas would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Wow that totally blows then.

Is there some simple alternative other than having to do try/catch?

Share this post


Link to post
Share on other sites
In practice there''s no method for determining if a given pointer is valid. About the best you can do, while staying in standard C++, is to move to a handle based system or smart pointers.

Share this post


Link to post
Share on other sites
Try using a reference system, everytime an object refers to (points to the object) then obj.addref() (ie ref++);
every time an object drops the reference then have it obj.deleteref() (ie ref--) before the drop. When you go to delete the object, check to see what the ref count is, if there are still objects refering it, then don''t delete it.

There are other means as already mentioned, such as smart pointers and handles etc.

Share this post


Link to post
Share on other sites
Smart pointers... seem too good to be true almost.

What is the catch? And if there is no catch, why aren''t they always used? I''ve never even heard of them until now, granted I am a newb, but I''ve seen many programs that stick to regular pointers when these smart pointers seem perfect.

Share this post


Link to post
Share on other sites
quote:
Original post by Kranar
Smart pointers... seem too good to be true almost.

What is the catch? And if there is no catch, why aren''t they always used? I''ve never even heard of them until now, granted I am a newb, but I''ve seen many programs that stick to regular pointers when these smart pointers seem perfect.

Because if you don''t need them, it''s just an extra wrapper to deal with around your pointers. However in special circumstances (like yours) they help out immensely.

Share this post


Link to post
Share on other sites
Smart pointers is the way to go. I don''t use smart pointers so I can''t answer your question there, but...

I question the design issue here. Why would other objects still accessing these deleted objects? Why do you delete objects while other objects still rely on them?

Share this post


Link to post
Share on other sites
<< I question the design issue here. Why would other objects still accessing these deleted objects? Why do you delete objects while other objects still rely on them? >>

Sort of a long story...

Basically I have a scripting language in my game, and this scripting language allows you to access objects in my game, like longswords or apples and stuff. When the longsword or apple gets destroyed, the scripting language doesn''t know this, so if some variable that was pointing to the apple is used, my game crashes.

Share this post


Link to post
Share on other sites
Keep in mind that I''m not bashing smart pointers in the rest of this point. They are tools with their uses. However, as for why smart pointers aren''t used everywhere:

Keeping the discussion limited to reference tracking smart pointer mechanisms, smart pointers can have quite a bit of overhead. In terms of memory requirements, this can range from a single word per object allocated to two words for every smart pointer per object allocated. (The latter case is for reference linked smart pointer representations, which tend to be uncommon.) One of the most common reference counted smart pointers, boost::shared_ptr uses an extra word of storage per pointer to allocated object, as well as allocates an additional 8 to 32 bytes on the heap, depending on underlying heap system. This impacts performance both by increasing memory requirements, but also by fragmenting the heap. Also, copying or assigning reference tracking smart pointers is significantly more expensive that copying/assigning a plain pointer.

Also, reference tracking smart pointers traditionally do not play well with threading.

Reference tracking smart pointers also can lead to a false sense of security with respect to memory leaks. For example, it''s possible for circular references to form, when memory is never released because two objects have smart pointers to each other and so never trigger deletion.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!