How comes this code leaks memory?

Started by
9 comments, last by Juliean 10 years, 11 months ago

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?

Advertisement

class DrawFont : public DrawCall
...

Does DrawCall have virtual destructor?

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.

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

You need a virtual destructor for any class that you intend to inherit from. Or at least inherit publicly, which is the only type of inheritance I would use.

In your case, the member `stText' of type `std::wstring' probably holds a pointer to dynamically allocated memory. If you don't call the destructor for the right class, this member's destructor won't get called and you'll get a memory leak.

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.

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.

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.

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

This topic is closed to new replies.

Advertisement