Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Wavarian

Memory leak: GetDC()

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, I''ve run into a little snag that I hope someone here can help me with. I''m working on an MFC app, and retrieve a window device context handle using the following method: HDC m_hDC = ::GetDC(m_hWnd); When the window is about to be destroyed, I release the handle using: ::ReleaseDC(m_hWnd,m_hDC); but a memory leak is found. Any ideas? It''s really driving my up the wall, and though I''ve tried other methods discussed in past threads, it''s still not working out. Thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
> HDC m_hDC

Seems like you''re having both a local called "m_HDC" and a member called "m_hDC".

Share this post


Link to post
Share on other sites
You might want to look into using CWindowDC as well.


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud

Share this post


Link to post
Share on other sites
No, m_hDC is a member variable that I''ve specified myself.


class CWindow : public CFrameWnd{
HDC m_hDC;
}


CFrameWnd does not have a m_hDC variable specified.

As for using CWindowDC, I''ve used CClientDC in the past and it has worked just fine, however would like to know why the method I''m using now produces memory leaks. If it''s a known problem, I''ll just go back to using CClientDC.

Thankyou for your replies, they''re appreciated, however I''d still like to know a reason for the above not to work.

Share this post


Link to post
Share on other sites
At the spot where you call ::GetDC you should NOT put the HDC in front of the line, as in the sample snippet you showed us:

HDC m_hDC = ::GetDC(m_hWnd);

This will declare a local HDC, incidentally with the same name as your member var.

Share this post


Link to post
Share on other sites
quote:
Original post by Endurion
At the spot where you call ::GetDC you should NOT put the HDC in front of the line, as in the sample snippet you showed us:

HDC m_hDC = ::GetDC(m_hWnd);

This will declare a local HDC, incidentally with the same name as your member var.


Oh sorry that was just an example, in my code m_hDC is defined as a member variable, and in a method, I assign it a value without specifying it''s type (thus the result is assigned to the member variable).

Share this post


Link to post
Share on other sites
Who says that a memory leak is found? If it''s MFC in the debug output, resource leaks are NOT mentioned there. The only tool that did detect those for me was BoundsChecker.

Gotta be somewhere else. Try to comment out some parts of the drawing code to narrow the place where the leaks occur.

Share this post


Link to post
Share on other sites
That''s the problem, this is the bare skeleton of my application. As soon as I added the use of GetDC() to retrieve a handle to the window device context, my MFC memory leak detection code reported a leak, and only when I comment out that line does everything go well. There isn''t any drawing code yet, I was planning to do some OGL initialization after getting the DC, but found a memory leak when I went to check if I was doing it right.

Loaded ''C:\WINDOWS\system32\uxtheme.dll'', no matching symbolic information found.
memory check error at 0x018D1CB0 = 0xD0, should be 0xFD.
memory check error at 0x018D1CB1 = 0x19, should be 0xFD.
memory check error at 0x018D1CB2 = 0x8D, should be 0xFD.
memory check error at 0x018D1CB3 = 0x01, should be 0xFD.
Detected memory leaks!
Dumping objects ->
{56} client block at 0x018D1BF0, subtype 0, 192 bytes long.
a CObject object at $018D1BF0, 192 bytes long
Object dump complete.
The thread 0x6F8 has exited with code 3 (0x3).

I''m just going to rewrite it from scratch tomorrow and find out if it still does the same thing. But honestly, this app doesn''t even do anything atm except for retrieving the DC - so I still don''t get where it could be going wrong.

Thanks for the help anyway.

Share this post


Link to post
Share on other sites
Hmmm, you say, you call ReleaseDC when the window is destroyed.
Exactly where are you calling ReleaseDC? Is the HWND still valid at that point?

What strikes me strange is this:

memory check error at 0x018D1CB0 = 0xD0, should be 0xFD.
memory check error at 0x018D1CB1 = 0x19, should be 0xFD.
memory check error at 0x018D1CB2 = 0x8D, should be 0xFD.
memory check error at 0x018D1CB3 = 0x01, should be 0xFD.

Somethings messing with invalid memory there. Maybe something writing where it shouldn''t and screws up the leak detection?

Share this post


Link to post
Share on other sites
Not sure if this will help but you should make sure your window is created with the CS_OWNDC attribute set.
Otherwise the OS creates a new DC each time any code calls GetDC() and one is already in use.

Also, why are you holding on the the DC handle?

Just call GetDC() when you need it, then Release it when you are done. Its not often this is needed throughout the life of the class.

Hope this helps.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!