Jump to content

  • Log In with Google      Sign In   
  • Create Account


using shared_ptr


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 bubbaray97   Members   -  Reputation: 213

Like
0Likes
Like

Posted 16 January 2013 - 03:49 PM

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?

 

 



Sponsor:

#2 rip-off   Moderators   -  Reputation: 8067

Like
5Likes
Like

Posted 16 January 2013 - 05:12 PM

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)

2000 items? That sounds insane!

 

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

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.



#3 bubbaray97   Members   -  Reputation: 213

Like
0Likes
Like

Posted 16 January 2013 - 05:29 PM

2000 items? That sounds insane!

 

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.

 

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

 

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)



#4 0r0d   Members   -  Reputation: 802

Like
0Likes
Like

Posted 16 January 2013 - 06:00 PM

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?



#5 Servant of the Lord   Crossbones+   -  Reputation: 18182

Like
3Likes
Like

Posted 16 January 2013 - 06:46 PM

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


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#6 bubbaray97   Members   -  Reputation: 213

Like
0Likes
Like

Posted 16 January 2013 - 10:13 PM

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?

 

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

 

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



#7 0r0d   Members   -  Reputation: 802

Like
1Likes
Like

Posted 16 January 2013 - 11:30 PM

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, 16 January 2013 - 11:32 PM.


#8 sednihp   Members   -  Reputation: 241

Like
2Likes
Like

Posted 17 January 2013 - 09:40 AM

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, 17 January 2013 - 09:40 AM.


#9 rip-off   Moderators   -  Reputation: 8067

Like
2Likes
Like

Posted 17 January 2013 - 10:13 AM

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

 

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.



#10 Servant of the Lord   Crossbones+   -  Reputation: 18182

Like
1Likes
Like

Posted 17 January 2013 - 12:33 PM

Couldn't one also:

GUIButton button(/* ... */);
</p><div>button->SetShortCut(ALLEGRO_KEY_L);</div>
<div>button->SetZOrder(1);</div>
UIelements.push_back(std::move(button));

 

Or will that not perform as well?


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                            [Need web hosting? I personally like A Small Orange]
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#11 bubbaray97   Members   -  Reputation: 213

Like
0Likes
Like

Posted 17 January 2013 - 07:41 PM

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



#12 Matt-D   Crossbones+   -  Reputation: 1429

Like
0Likes
Like

Posted 18 January 2013 - 08:06 PM

(cleaned)

Edited by Matt-D, 18 January 2013 - 08:23 PM.


#13 Matt-D   Crossbones+   -  Reputation: 1429

Like
2Likes
Like

Posted 18 January 2013 - 08:20 PM

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



#14 rip-off   Moderators   -  Reputation: 8067

Like
2Likes
Like

Posted 19 January 2013 - 07:40 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS