Sign in to follow this  

[Solved] Global shared_ptr

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

This is a question that *really* makes me feel like a newbie, but...can anyone suggest a way to have a global boost::shared_ptr? My game uses only shared_ptrs, no raw pointers, but none of the global objects are getting deleted, since they're always defined as so: --header file: extern shared_ptr<Thingy> gCoolThingy; --file scope in a .cpp file: shared_ptr<Thingy> gCoolThingy; --in actual code for the first time: gCoolThingy.reset(new Thingy()); Is this a totally stupid question -- is "global shared_ptr" an oxymoron? [Edited by - Sol Blue on January 4, 2008 9:22:48 PM]

Share this post


Link to post
Share on other sites
Global shared pointers well be deleted when the program finishes execution provided the program exits normally by execution continuing off of the end of the main() function.

Share this post


Link to post
Share on other sites
exit() may call some destructors. Usually for objects with static allocation, which includes globals, but it generally doesn't call the destructors for objects on the stack.

Share this post


Link to post
Share on other sites
Thanks for the responses. I do indeed do a normal return out of my main function.

Quote:
Original post by MaulingMonkeyHow are you determining they aren't getting deleted?


I have a breakpoint in one of the destructors, and by breaking on a bunch of memory allocations that the debugger says is leaking.

Share this post


Link to post
Share on other sites
How are you getting the debugger to tell you memory leaks? Some leak detection methods don't properly detect deletion of memory cleared by globals.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
How are you getting the debugger to tell you memory leaks? Some leak detection methods don't properly detect deletion of memory cleared by globals.


I have this code in my main function:


#if defined(DEBUG) | defined(_DEBUG)
//_crtBreakAlloc = 49;
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif



Then in Visual Studio's output window, at the end of the program, I get stuff like this:


Detected memory leaks!
Dumping objects ->
c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {298} normal block at 0x00FA6CE0, 16 bytes long.
Data: <Hi Hi f h > 48 69 FA 00 48 69 FA 00 FC 66 FA 00 F8 68 FA 00
c:\program files\microsoft visual studio .net 2003\vc7\include\crtdbg.h(689) : {297} normal block at 0x00FA6C90, 16 bytes long.



I think I may have a cyclical dependency somewhere in my shared_ptrs, though, so it may not be a problem with the components being global. : / I'm trying to untangle it ATM. I'll post again when/if that proves to be the solution.

Share this post


Link to post
Share on other sites
You don't need the #if around the _CrtSetDbgFlag() call. _CrtSetDbgFlag() is a macro that compiles out to nothing in non-debug builds. In any case, that method should only trip on legitimate leaks given that your execution continues off of main().

Share this post


Link to post
Share on other sites
Oh, thanks -- I'll take the DEBUG guards out then.

I think I might have solved my problem...things are tentatively looking good without that cyclic dependency in there.

Thanks for everyone's help -- sorry for kinda wasting your time. :(

Share this post


Link to post
Share on other sites

This topic is 3633 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this