# A Few questions about boost::shared_ptrs

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

## 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 on other sites
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 on other sites
Ahhhhh, yeah, that syntax makes a lot more sense and is a lot less cumbersome. Thanks for that insight.

##### Share on other sites
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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634062
• Total Posts
3015303
×