Jump to content
  • Advertisement
Sign in to follow this  
Antrim

A Few questions about boost::shared_ptrs

This topic is 4833 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've been using boost::shared_ptrs for a while now, but I'm now beginning to wonder if there are cases where I'm using them improperly. My biggest concern is if there are any cases when you may accidently delete the object being pointed to because of working around the reference count. For example, if I have a member function in my game engine: boost::shared_ptr<T> GetT(){return T;} Now if I call this function from my game test code, such as: void FooFunction(){ boost::shared_ptr<T> Foo; Foo = pEngine->GetT(); //do stuff with Foo here return; } My question is if this assigment, or any other assignment similar to it, would bypass or mess with the reference count in the shared_ptr so that it improperly deleted the object when the function returned. I'm pretty sure that this particular case wouldn't have any ill effects, but I'm wondering if there is some other, more subtle, type of assignment that would. Also, unrelated to the last question, was a problem I had with the sytax of the boost pointers. I was trying to rewrite a function originally defined as: Foo((void**)theScript); I tried rewriting this using shared_ptrs as: Foo(boost::shared_ptr< boost::shared_ptr<void> > theScript) but when I tried to call the function, boost::shared_ptr< boost::shared_ptr<Resource> > test(new Resource); Foo(test); the compiler would spit out errors. Is there some issue that I'm unaware of with shared_ptrs using void*s? or am I just simply doing something incorrect in the definition/calling of the revamped function? I have had some other unexpected things happening with some of my code in regards to using the shared_ptrs before. Since that obviously means I don't have a thourough understanding of them, I just thought I'd ask about any subtle issues that I need to be aware of. Thanks for any input

Share this post


Link to post
Share on other sites
Advertisement
Use this for pointer to void:


boost::shared_ptr<void*> Foo(p);


Where p is a void** etc.

As for reference counts I can't say but I am pretty sure it is well design enough to handle it. shared_ptr was designed (I think) for containers because auto_ptr is not safe for that ( and is supposedly coded to break when you use it in containers on some platforms).

Share this post


Link to post
Share on other sites
Ahhhhh, yeah, that syntax makes a lot more sense and is a lot less cumbersome. Thanks for that insight.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Another question just came to mind that I can't seem to find the anser to.

I'm using intrusive_ptrs to handle my DX Devices now. But when creating the devices, like D3D for example, I need to pass an IDirect3DDevice ** to the CreateDevice function.

Normally, I'd create an IDirect3DDevice* and then pass a reference to it. However, if I am using a boost::intrusive_ptr, I can't do a "&(myBoostPtr.get())", because I get errors about "&" needing an l-value.

I know that I can bypass this by first creating a normal IDirect3DDevice*, passing it into the CreateDevice function as normal, and then assigning it to the boost pointer. However it seems like there should be some way to do it a bit more elegantly, without having to create the extra pointer.

Anyone have any knowledge of the sytax I should use to do this?

Thanks for any help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!