Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

RAII and smart pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Jarwulf   Members   -  Reputation: 183

Like
0Likes
Like

Posted 13 May 2012 - 09:54 PM

Can someone give me a few simple to understand examples of RAII and smart pointers? I read somewhere that RAII and smart pointers should be used for everything but other places say that pointers are useless and you should use references instead. For my part I don't know of any uses for pointers aside from passing values between functions economically or iterating through a linked list. Any major uses beside those?

Also in the examples of RAII I'm looking at all it is seems to be a class file with an empty constructor and empty destructor. Is it really that simple?

Edited by Jarwulf, 13 May 2012 - 09:56 PM.


Sponsor:

#2 Trienco   Members   -  Reputation: 1337

Like
0Likes
Like

Posted 13 May 2012 - 10:55 PM

Uhm... try to use polymorphism without pointers. Ok, references, but how often do you see code like
Object& obj = *(new DerivedObject);

Not to mention the cleanup:
delete &obj;

Besides, there is no such thing as "smart references" to help you avoid memory leaks and accessing invalid objects. Apart from that, pointers and references have only a few differences under the hood. It's not like they are doing completely different things. References are more convenient in syntax and unless someone deliberately does something stupid you shouldn't have to worry about them being valid.

RAII examples? Sure. std::fstream, std::vector and pretty much anything that keeps track of its resources and releases them when going out of scope. Like scoped_lock or unique_ptr.

If the constructor and destructor is really empty, it has little to do with RAII. Apparently the object doesn't "acquire" any resources during initialization and has nothing to release on destruction. That would be like calling an empty chassis an example of a light weight car.


An example for RAII and smart pointers.

//without
char* tmpBuffer = new char[chunkSize];
mutex.lock();

loadData(tmpBuffer);

mutex.unlock();
delete[] tmpBuffer;

Oh no, loadData just threw an exception. I must ensure proper cleanup in all cases.


//without
char* tmpBuffer = new char[chunkSize];
mutex.lock();

try
{
    loadData(tmpBuffer);
}
catch (...) //and I don't really know what kind of exception to expect
{
	 mutex.unlock();
	 delete[] tmpBuffer;
}
mutex.unlock();
delete[] tmpBuffer;


How about doing it the smart way (without a mess)?

vector<char> tmpBuffer(chunkSize);
scoped_lock lock(mutex);

loadData(&tmpBuffer[0]);

See? No cleanup, no mess to deal with exceptions, no chance of someone screwing it up by spamming early returns all over your function.
f@dzhttp://festini.device-zero.de

#3 turch   Members   -  Reputation: 580

Like
0Likes
Like

Posted 14 May 2012 - 07:31 AM

I generally don't use "pure" smart pointers (reference counted pointers that delete themselves) because in my opinion they muddle the concept of what owns the object and manages its lifetime. For most objects that have a complex lifetime that would require smart pointers, I find it makes sense to have something manage the lifetime of that object. That can involve reference counting pointers, but not ones that delete themselves automatically.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS