-
February 2nd, 2011, 10:12 AM
#1
Does GetDC()->operator HDC() need a ReleaseDC?
Hi all,
I am calling a DLL function OnMouseMove which requires me to pass an HDC.
I am using:
Code:
OnMouseMove{
HDC hdc;
hdc = GetDC()->operator HDC();
DllFunc(hdc);
...
}
I don't see any memory leaks but the documentation suggest that when using GetDC(), a ReleaseDC() must follow.
Is this accurate?
Should i rewrite my function as below?
Code:
OnMouseMove{
HDC hdc;
CDC *pdc;
pdc = GetDC();
hdc = pdc->operator HDC();
DllFunc(hdc);
ReleaseDC(pdc);
...
}
Thanx in advance!
-
February 2nd, 2011, 11:01 AM
#2
Re: Does GetDC()->operator HDC() need a ReleaseDC?
GetDC without ReleaseDC may create resource leak, and not memory leak. The second code version is correct.
-
February 2nd, 2011, 11:07 AM
#3
Re: Does GetDC()->operator HDC() need a ReleaseDC?
ReleaseDC is only necessary for common DC's.
If the window owns a DC (private or class dc) ReleaseDC is not necessary (although recommended).
So yes, the 2nd version is prefered, although you can write it more compact.
Code:
CDC *pdc = GetDC();
DllFunc(pdc->GetSafeHdc());
ReleaseDC(pdc);
-
February 2nd, 2011, 11:13 AM
#4
Re: Does GetDC()->operator HDC() need a ReleaseDC?
And to avoid the problems with ReleaseDC - just don't use GetDC at all!
There is the CClientDC class (MFC) which implicitly calls GetDC in its ctor and ReleaseDC in the dtor. So
Code:
CClientDC dc;
hdc = dc.operator HDC();
DllFunc(hdc);
...
Victor Nijegorodov
-
February 3rd, 2011, 02:35 AM
#5
Re: Does GetDC()->operator HDC() need a ReleaseDC?
Thanx guys!
You have all been very helpful.
-
February 3rd, 2011, 08:53 AM
#6
Re: Does GetDC()->operator HDC() need a ReleaseDC?
I also would suggest to call GetSafeHdc() (like in OReubens post) instead of operator HDC().
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|