• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bubbaray97

using shared_ptr

13 posts in this topic

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?

 

 

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites
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
2

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites

Couldn't one also:

[code]GUIButton button(/* ... */);

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

 

Or will that not perform as well?

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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

2

Share this post


Link to post
Share on other sites

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.

2

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  
Followers 0