Jump to content
  • Advertisement


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


How useful are smart pointers?

This topic is 6723 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

I''m sure most of you have seen the very informative article posted here entitled: "Low Latency Garbage Collecting via Reference Counting". My question is this: how useful is this? I mean besides sparing me from making sure all of my ''new'' calls have corresponding ''delete'' calls, just how useful are smart pointers? Am I missing the point, or do smart pointers serve some other purpose that I just don''t see. Granted, the simplicity of an object that deletes itself is nice, but at the expense of another level of encapsulation? Is this really neccessary? I don''t mean to put down the authour, Photon, by any means as his article was in my opinion, very well written and informative. I am only curious to see if anybody out there actually uses this in practice.

Share this post

Link to post
Share on other sites
I had the same question a while ago, but I think I''ve found some uses for them...

1) STL Containers

Let''s just say, for example, that you have written an image class, and it holds a DirectDraw surface pointer and manages all of that for you. On construction, you create the surface, load in the image, and you''re set. On destruction, you just Release() the surface.

But the problem arises when you want to use that image class in a STL container, like std::vector. If you don''t define a copy constructor and assignment operator for the image class, then a shallow copy is performed. In that particular instance, we have two pointers to the same surface, and both image classes (the temp and the one in std::vector) will try to deallocate the same object.

By creating a copy constructor and assignment operator, you can create a second surface and simply blit the other surface to it. However, this copying creates overhead. This simple, unassuming call involves the creation of one surface, the creation of another surface, and the destruction of the first surface:

images.push_back(image(1, ".\\grass.bmp"));

What does all of this have to do with smart pointers? Well, by making a vector of smart pointers, you avoid the copy operation and only ever use one surface. With certain classes, the copying/allocation/deallocation could be quite slow. With smart pointers, the only object ever copied is a smart pointer object, which is a trivial operation.

2) auto_ptr

The STL includes a class called auto_ptr, which simply wraps a heap pointer in a stack object. Why is this useful? When you allocate memory in a function, and an exception is thrown somewhere between the allocation call and deallocation call, the memory will not be deallocated. So, by wrapping it in an auto_ptr and releasing it when you are done, the memory will be deallocated even in the event of an exception:

void do_something()
image* temp_image = new image(1, ".\\grass.bmp");

throw "leaking memory...";

delete temp_image;

Here is a much better version:

void do_something()
auto_ptr temp_image(new image(1, ".\\grass.bmp");

throw "not leaking memory...";

In version two, the auto_ptr destructor will deallocate the memory when the function exits, even if the function exits due to an exception.

3) Language independence

This is all about COM. IUnknown, the root of all COM objects, provides, among other things, a smart pointer that is completely independent of a computer language.

Why is a smart pointer needed? Since the implementation of the object cannot be known by the user, and even the construction cannot be known (because of language independence), the allocation/deallocation must be wrapped in CoCreateInstance() and IUnknown::Release().

Good Luck!

- null_pointer
Sabre Multimedia

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!