Jump to content

  • Log In with Google      Sign In   
  • Create Account

How comes this code leaks memory?


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
10 replies to this topic

#1 Juliean   GDNet+   -  Reputation: 2596

Like
0Likes
Like

Posted 15 May 2013 - 07:30 AM

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?



Sponsor:

#2 belfegor   Crossbones+   -  Reputation: 2611

Like
2Likes
Like

Posted 15 May 2013 - 07:38 AM

class DrawFont : public DrawCall
...

Does DrawCall have virtual destructor?



#3 Dave Hunt   Crossbones+   -  Reputation: 2457

Like
4Likes
Like

Posted 15 May 2013 - 07:40 AM

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.



#4 Juliean   GDNet+   -  Reputation: 2596

Like
0Likes
Like

Posted 15 May 2013 - 07:47 AM

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, 15 May 2013 - 07:57 AM.


#5 Álvaro   Crossbones+   -  Reputation: 13308

Like
4Likes
Like

Posted 15 May 2013 - 07:55 AM

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.



#6 Dave Hunt   Crossbones+   -  Reputation: 2457

Like
2Likes
Like

Posted 15 May 2013 - 07:57 AM

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, 15 May 2013 - 08:09 AM.


#7 Orangeatang   Members   -  Reputation: 1498

Like
2Likes
Like

Posted 15 May 2013 - 07:58 AM

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, 15 May 2013 - 08:01 AM.


#8 Álvaro   Crossbones+   -  Reputation: 13308

Like
3Likes
Like

Posted 15 May 2013 - 08:09 AM

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.



#9 NightCreature83   Crossbones+   -  Reputation: 2823

Like
2Likes
Like

Posted 15 May 2013 - 08:12 AM

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, 15 May 2013 - 08:13 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#10 Dave Hunt   Crossbones+   -  Reputation: 2457

Like
2Likes
Like

Posted 15 May 2013 - 08:13 AM

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.



#11 Juliean   GDNet+   -  Reputation: 2596

Like
0Likes
Like

Posted 15 May 2013 - 08:42 AM

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, 15 May 2013 - 08:53 AM.





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