• 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
Juliean

How comes this code leaks memory?

10 posts in this topic

Hello,

 

I've come across one rather strange kind of memory leak. I always thought memory leaks where caused if you forget to delete a resource you allocated, or loose a pointer to it, but this one... well, look:

 

 

        void Text::Draw(render::Stage& stage, const math::Rect& rect, LPCWSTR lpText, DWORD dFlags, float f)
        {

            math::Rect r(rect);
            r.width += rect.x;
            r.height += rect.y;

/* Leak: */
            delete m_pCall;

            m_pCall = new render::DrawFont(m_pActiveFont->GetAclFont().m_lpFont, lpText, r.GetWinRect(), 
dFlags, m_color.GetD3DColor());

/* End Leak */
            
            m_pInstance->SetCall(*m_pCall);

            stage.SubmitInstance(*m_pInstance);
        }
 

So, those two lines cause the leak. If I draw some text using this (unoptimized, hackaround-like) font rendering method for my renderer, memory used by the application will rise continously in small amounts noticable in the task manager. Here is how my DrawFont-method is declared:

 

		class DrawFont : public DrawCall
		{
		public:

            DrawFont(const LPD3DXFONT font, const std::wstring& stText, const RECT& rect, DWORD flags, D3DCOLOR color): stText(stText), font(font), rect(rect), 
                flags(flags), color(color) { type = Font; }

			void Execute(const DX9Device& device) const
			{
                RECT r(rect);
				font->DrawTextW(nullptr, stText.c_str(), stText.size(), &r, flags, color); 
			}

        private:

            RECT rect;
            DWORD flags; 
            std::wstring stText;

            D3DCOLOR color;
            const LPD3DXFONT font;
		};

 

Now uhm... I don't really see any reason why this could leak memory. I'm deleting the draw call I allocated before, I'm not newing anything in between those two actions concerning the DrawFont-class, and the general render interface doesn't show this leaking behaviour, its only the text-class. Also it has nothing to do with calling the "Execute"-function, even if I comment out the part in my render queue where this call is processed, it still leaks. As soon as I comment out that two lines, no such rise in memory. Am I really that stupid, or is there some good reason why memory usage could rise by those simple actions and/or something I missed out?

0

Share this post


Link to post
Share on other sites

Does DrawCall have virtual destructor?

 

Uhm, no, it doesn't, but since there is nothing I new'd inside of DrawFont, I recalled I didn't need it? Obviously adding it solves the memory leak problem but... explanation please? As said, I thought you only need a virtual destructor if you need want to handle something inside of a inherited classes constructor?

 

Is your base class' destructor virtual? If not, then deleting an instance through a pointer to the base class will only delete the base class memory, thus causing a leak.

 

Ah, I see, didn't know that. So there really is little reason not to have a virtual destructor, isn't it... thanks anyway, time for some in-depth analysis where else I screwed up on this :/

Edited by Juliean
0

Share this post


Link to post
Share on other sites

Since m_pCall was declared as a pointer to the base class, and since the destructor was not declared virtual, only the base class destructor gets called. That means that any space allocated for the derived class over and above what is in the base class won't get freed.


edit -- correction - the derived class destructor gets called, freeing the memory allocated in the derived class, but the base class destructor doesn't get called, thus leaking any memory allocated in the base class. Officially, the behavior is undefined, so it really doesn't matter which one leaks. Just know that you will leak memory.

Edited by Dave Hunt
2

Share this post


Link to post
Share on other sites

nothing I new'd inside of DrawFont

But if there's something you've allocated in the DrawCall class, then it's not going to be de-allocated when you delete your DrawFont instance.

 

For a parent class's destructor to be called when you delete a derived class, said desturctor has to be virtual.

Edited by Orangeatang
2

Share this post


Link to post
Share on other sites
You realise that looking at the memory in the task manager is not a good indication to check if a memory leak is present. Install Valgrind or Visual Leak Detector to make sure you really have a memory leak or not. A never ending recursive function would show the same thing in task explorer until it hits the stack limit and then it will crash.

General rule of thumb in C++ is as soon as you add a derived class make the destructor of the base class virtual! Once a function is marked virtual it will be virtual for all derived classes of that class. Edited by NightCreature83
2

Share this post


Link to post
Share on other sites

Since m_pCall was declared as a pointer to the base class, and since the destructor was not declared virtual, only the base class destructor gets called. That means that any space allocated for the derived class over and above what is in the base class won't get freed.

 

You said this twice, but I don't think it's precise. Memory is allocated in blocks, and there is no such thing as freeing part of a block. I am not going to check the standard, but there is a good chance that deleting an object through a pointer that doesn't have the same type that was used when `new' was called is undefined behavior. But the practical reason why his code is leaking is that DrawFont has a member that uses dynamic memory allocation, whose constructor needs to be called for proper cleanup. If DrawFont only added plain-old-data members, this would probably not happen.

Yes, my post was a bit imprecise. I meant things dynamically allocated in the derived class, not the block of memory allocated for the object.

2

Share this post


Link to post
Share on other sites

Thanks all for the additional explanation!

 

 

You realise that looking at the memory in the task manager is not a good indication to check if a memory leak is present. Install Valgrind or Visual Leak Detector to make sure you really have a memory leak or not. A never ending recursive function would show the same thing in task explorer until it hits the stack limit and then it will crash.

 

Yeah, sure, I wasn't even looking for a memory leak, I just happened to be looking at the task manager the right time and noticed that rise in memory. Turned out well this time, surely if I was really going after memory leaks I'd check out a tool for that case, thanks for the hints. Still, a endloss loop or recursive function is out of the question for this case, since, well, the the app was still running (no threads), and normally such a function would cause the app to die almost instantly (at least started with Debugger from Visual Studio), so I don't think its that bad as a general sign of some sort of memory related problem...

Edited by Juliean
0

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