using shared_ptr

Started by
12 comments, last by rip-off 11 years, 3 months ago

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?

Advertisement

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)

[/quote]

2000 items? That sounds insane!

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

[/quote]

Nothing obvious, apart from maybe the above. One consideration is whether all your GUI objects need "shared" ownership, can some of these be unique pointers? What about value objects? If you really need 2,000 buttons, perhaps a vector<GUIButton> would be better. Or you may want to consider an "Immediate GUI", if only for these buttons (particularly if it is an in-game UI rather than a traditional U.I. "screen").

There are three major pieces of overhead that reference counted smart pointers could add to your code:

  • Allocation becomes more expensive because there are now two allocations per object, unless you use make_shared.
  • Deallocation actually happens. Doesn't sound like that happened reliably in your previous system. But like the previous entry, deallocations are more expensive because there are two deallocations to perform.
  • Copying, assigning and destructing the shared pointers incurs changes to the reference counts.
  • For a GUI, one would expect that allocation and deallocation would be "rare" affairs. That is, once your GUI is loaded you shouldn't be allocating or deallocating every frame. One would expect all the deallocation as the GUI screen is discarded.

    Copying overhead can often be avoided. If you aren't trying to transfer ownership, consider passing the objects by reference to functions.

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

    [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)

    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?

    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).

    [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...

    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.

    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

    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:

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

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

    Couldn't one also:

    GUIButton button(/* ... */);

    button->SetShortCut(ALLEGRO_KEY_L);
    button->SetZOrder(1);
    UIelements.push_back(std::move(button));

    Or will that not perform as well?

    This topic is closed to new replies.

    Advertisement