Sign in to follow this  
bubbaray97

using shared_ptr

Recommended Posts

bubbaray97    217

ok please bear with me on this one smile.png

 

For my game I've been building my own GUI library (mostly because I wanted to learn how and partly because the ones available for Allegro 5 were either overkill or not good enough)

 

I was lazy when I started this and used regular old raw pointers, but as my game itself has become more complex I started noticing memory leaks so I just went through the (painful) experiance of changing all my pointers to std::shared_ptrs

 

[Side-Note to self and to others do it right the first time, that was a pain]

 

Anyway the memory leak is gone now which is great, but my frame-rate dropped from the 60 FPS I was throttling too down to 10 FPS (sometimes dipping to 2 FPS)

 

Now I expected sharted pointers to be a bit slower, but thats crazy.    I have to assume that it is me using them wrong.

 

As an example here is how I use this...

 

GUIElement is my superclass that all the widgets base themselves on and then I have a vector to hold them all...

 

std::vector<std::shared_ptr<GUIElement>> UIelements;
std::vector<std::shared_ptr<GUIElement>>::iterator UIiter;

 

When I want to put something on the screen I do something like this...

 

    std::shared_ptr<GUIButton> button1(new GUIButton(g_width/2-125,g_height/2-125,0,250,100,"(L)oad Default Scenario",updatestate));
    button1->SetShortCut(ALLEGRO_KEY_L);
    button1->SetZOrder(1);
    UIelements.push_back(button1);

Now I may do that 2000 times on a single screen for all the various elements on the map (which worked fine as "raw" pointers)

 

In my main game loop I have this...

 


///in the game logic update part of the loop....

    for(UIiter=UIelements.begin();UIiter!=UIelements.end();++UIiter)
        (*UIiter)->Update();

//in the render part of the game loop

    for(UIiter=UIelements.begin();UIiter!=UIelements.end();++UIiter)
    (*UIiter)->Render();

 

 

Anyway anything obvious that I"m doing wrong here, or some thoughts on what to look into?

 

 

Share this post


Link to post
Share on other sites
bubbaray97    217

[quote name='rip-off' timestamp='1358377964' post='5022345']
2000 items? That sounds insane!
[/quote]

 

Well not all 2000 are buttons....yeah that would be crazy.    But like GUIButton, I have GUIImage, GUIText, etc.

The bulk of the GUIElements are the GUIImage objects for the mapcells (its a 61x19 map of hexagons) and then the images for units, etc.

 

[quote name='rip-off' timestamp='1358377964' post='5022345']

Of course, you can get the actual answer by profiling your code to see where the time is spent.

[/quote]

 

yeah I just downloaded some profilers (I have the Express version of VS) but since the only change between the two builds was the switch to smart pointers figure thats the cause.

 

Thanks for the advice I'm going to take a closer look at when/where my deallocations are happening (given the memory leak that lead me here thats probably part of the problem anyway)

Share this post


Link to post
Share on other sites
0r0d    1930

Are you testing in release or debug?

 

You need to profile the two versions to know what's going on.  How much CPU time is being spent and in what functions, before and after?

Share this post


Link to post
Share on other sites
I was lazy when I started this and used regular old raw pointers, but as my game itself has become more complex I started noticing memory leaks so I just went through the (painful) experiance of changing all my pointers to std::shared_ptrs

Did you find and fix the memory leaks before you switched to std::shared_ptrs, or is switching to shared_ptrs a gigantic band-aid fix instead of actually finding the problem? smile.png
You openly admit to being lazy "when I started" - is it possible that this is more laziness, and did you actually research what smart pointers are for?

Smart pointers don't replace regular pointers, they supplement them. You'll still find uses for raw pointers - they haven't been removed from the language.

Thirdly, shared_ptrs aren't the only type of smart pointers in C++. There are also unique_ptrs and weak_ptrs. (auto_ptr has been deprecated, thankfully, so you can completely ignore that). You need to learn the pros and cons of all six: Regular variables, references, raw pointers, shared_ptrs, unique_ptrs, and weak_ptrs.

 

Anyway the memory leak is gone now which is great, but my frame-rate dropped from the 60 FPS I was throttling too down to 10 FPS (sometimes dipping to 2 FPS)

Now I expected sharted pointers to be a bit slower, but thats crazy. I have to assume that it is me using them wrong.

 

Sometimes the "wrong" is not the way you use them, but using them at all in inappropriate situations. I already mentioned, but it bears repeating, shared_ptrs aren't a "replace every pointer with shared_ptr" magic bullet, otherwise C++ would've just removed raw pointers and used the same syntax for shared_ptrs. There are cons and they need to be understood.

Tips:

  • If you let C++ manage your 'deletes' for you, why are you still managing the 'new'? Use std::make_shared when creating a shared_ptr.
  • If there is only ever one pointer that owns an object, unique_ptr might be a better choice.
  • When possible, creating objects on the stack (i.e. no dynamic allocation) is almost always preferred.
  • Just because it's a shared_ptr, that doesn't mean you should pass the shared_ptr object itself by value - do it by reference or const reference.
  • If you are suffering from performance issues, consider saving the GUI objects and re-using them, instead of de-allocating the object, de-allocating the pointer, allocating a new object, and allocating a new pointer.


Smart pointers are very very useful tools, I'm not saying that are bad to use, they just (like any tool) need to be understood. Even a hammer has a proper way to handle it, you don't just pick it up and bang on nails (speaking as someone who has actually used hammers and has difficulty mastering them).
 

Share this post


Link to post
Share on other sites
bubbaray97    217

[quote name='Servant of the Lord' timestamp='1358383598' post='5022387']
Did you find and fix the memory leaks before you switched to std::shared_ptrs, or is switching to shared_ptrs a gigantic band-aid fix instead of actually finding the problem?
[/quote]

 

Touche :)

 

Certainly appears you're right.   Yes I have researched what smart pointers were for, but in my haste I now realise I went for the band-aid.    Anyway I'll be digging through my code now to find the "real" problem and address it the proper way.

 

Thank you for your list of tips, I really do appreciate it.   (also thanks for pointing out what should have been obvious to me in the first place :))

 

[quote name='0r0d' timestamp='1358380838' post='5022367']
Are you testing in release or debug?

You need to profile the two versions to know what's going on. How much CPU time is being spent and in what functions, before and after?
[/quote]

 

Just to answer the questions I mostly do everything in debug mode.    Turns out running the profiler when I"m using the shared pointers about 32% of my program execution time is spent in std::swap<GUIElement *>  whereas with the raw pointers no single call gets above 5% of execution time (_RTC_CheckStackVars is the highest if you care to know).

 

Anyway gives me somewhere to consider.    Although having read everyones input here I think what I need to do is look more closely at how I'm passing around my GUIElements and verify how/when my allocations and deallocations *should* be occuring.

 

Thank you all...

Share this post


Link to post
Share on other sites
0r0d    1930

Are you testing in release or debug?

You need to profile the two versions to know what's going on. How much CPU time is being spent and in what functions, before and after?

 

 

Just to answer the questions I mostly do everything in debug mode.    Turns out running the profiler when I"m using the shared pointers about 32% of my program execution time is spent in std::swap<GUIElement *>  whereas with the raw pointers no single call gets above 5% of execution time (_RTC_CheckStackVars is the highest if you care to know).

 

Anyway gives me somewhere to consider.    Although having read everyones input here I think what I need to do is look more closely at how I'm passing around my GUIElements and verify how/when my allocations and deallocations *should* be occuring.

 

Thank you all...

 

I see.  Ok, so I have 2 pieces of advice for you:

 

1. Dont ever guess as to how some code will or is performing.  Always profile, either with dedicated profiling tools or with your own timing code.  Also, you need to profile your code with as close to final data as possible.  

 

2. Dont ever do performance testing in debug mode.  I made this bold so you will pay special attention to it.  You should test performance in either release mode, or whatever build mode you have dedicated to shipping or perf testing your game.  But a lot of times people just have debug and release, in which case you use release.

 

So before you seek other advice on this, or adjust your code in any way, stop what you're doing and test in release mode.  Any framerate or timing data you have from debug mode is completely meaningless.  I can say that without knowing anything about your code.

Edited by 0r0d

Share this post


Link to post
Share on other sites
sednihp    241
When I want to put something on the screen I do something like this...


std::shared_ptr button1(new GUIButton(g_width/2-125,g_height/2-125,0,250,100,"(L)oad Default Scenario",updatestate));
button1->SetShortCut(ALLEGRO_KEY_L);
button1->SetZOrder(1);
UIelements.push_back(button1);

Now I may do that 2000 times on a single screen for all the various elements on the map (which worked fine as "raw" pointers)

In my main game loop I have this...
Instead of using new, use std::make_shared<T> instead, i.e.

std::shared_ptr button1 = std::make_shared<GUIButton>(g_width/2-125, g_height/2-125, 0, 250, 100, "(L)oad Default Scenario", updatestate);
Make_shared is exception safe and uses the same call to allocate the memory for the reference counting block and the resource itself, reducing construction overhead (and hey, if you're doing something 2000 times you want to do it the most efficient way possible)

For more advice on how to use shared_ptr I found the MSDN article excellent (there are equivalent articles on unique_ptr, weak_ptr and general smart pointers as well)

If you didn't have to call setShortCut() and setZOrder() I would have recommended emplace_back as well,

UIElements.emplace_back(std::make_shared<GUIButton>(g_width/2-125, g_height/2-125, 0, 250, 100, "(L)oad Default Scenario", updatestate));
Which constructs the element in place in the vector Edited by sednihp

Share this post


Link to post
Share on other sites
rip-off    10976

[quote]

If you didn't have to call setShortCut() and setZOrder() I would have recommended emplace_back as well,

[/quote]

 

The calls could be moved after:

[code]UIElements.emplace_back(/* ... */); GUIButton &button = *UIElements.back(); button.SetShortCut(ALLEGRO_KEY_L);
button.SetZOrder(1);[/code]

Alternatively the GUIButton constructor could be overloaded to allow these attributes to be set during creation.

Share this post


Link to post
Share on other sites
bubbaray97    217

Thank you all so much.    I really apprceite all the input.    My "real job" had me busy today so didn't get to work on this yet today.   But last night i did at least notice one brain dead thing I was doing in my GUI creation loop.

 

Anyway before I do any more on the game itself I'm going to put some more work in on this "engine" part for awhile using your advice.   Hopefully here in a few days I can repost how its all better (and tested in release mode :))

 

Thanks

Share this post


Link to post
Share on other sites
Matt-D    1574

- std::make_shared is a great suggestion, you're not supposed to be using "new" when you know you're going to use an std::shared_ptr
- you should seriously consider std::unique_ptr -- I've noticed it was already mentioned several times, but you haven't stated your reasons for not using it; in fact, std::unique_ptr should be your default choice -- consider std::shared_ptr to be a more specialized smart pointer which you should only use if you're actually sharing resources
- unfortunately, there's no std::make_unique (as of C++11), free free to grab it from here -- http://herbsutter.com/gotw/_102/ -- a pretty good read, BTW

Last but not least, please carefully read this before proceeding any further: http://www.informit.com/articles/article.aspx?p=1944072


It's not a good sign that "changing all my pointers to std::shared_ptrs" was even considered, given that std::unique_ptr should be the first / default / preferred choice. IMHO, attempting to throw random smart pointers at the problem, without first reading about what the options and the associated trade-offs are (using "new" when constructing std::shared_ptr is also a bad sign), is pretty much an anti-pattern and rather risky. Dynamic memory management is not trivial; high-performance dynamic memory management is even less so. Consider yourself warned ;-)

Share this post


Link to post
Share on other sites
rip-off    10976

Another interesting point on the "changing all my pointers to std::shared_ptrs", this does not guarantee you will not get memory leaks. The standard shared_ptr cannot handle circular references.

 

That isn't to say that you are stuck in the case of circular references. Firstly, there are usually ways to design your system so that you do not need circular references. For the occasions one cannot do this, one can usually convert some of the shared_ptr<> into weak_ptr<> or into a non-smart pointer/reference (depending on the relative lifetimes of the objects involved).

 

The point is that, whether you plan to manage memory manually or semi-automatically, you still have to think about object ownership.

Share this post


Link to post
Share on other sites

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