Click to See Complete Forum and Search --> : what does it mean when ::CreateCompatibleDC returns 0
g3RC4n
October 11th, 2008, 02:19 PM
i've got a text editor i'm making where i'm creating and deleteing alot of objects for things like double buffering
now does it mean that there is a memory leak when something returns 0? because the program works for a while then my assertions will pick up the failure, which i assume is because there is a leak
or could it be that i shouldn't be createing and deleting hdc's or something (memory fragmentation?) but retian them
anyway can i assume theres just a simple leak?
Notsosuperhero
October 11th, 2008, 03:26 PM
Not too sure since you show no code.
But have you tried GetLastError() (http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx), I'm not sure if it catches errors from CreateCompatibleDC, but you can try and see if that comes up with anything.
g3RC4n
October 11th, 2008, 10:58 PM
k thanks i'll look into it
g3RC4n
October 12th, 2008, 03:02 AM
k i'm getting error code 5
msdn says it means "Access is denied.", does that mean that the hdc i'm trying to be compatiable with is wrong or something?
seems to also always be at the same point
HDC hdc_mem = ::CreateCompatibleDC(hdc);
now i know this isn't very helpfull on it's own but here it is anyway,
txt_rend is a text rendering class that i'm working on
void txt_rend::draw_buffer(HDC hdc, const std::wstring& wstr, int x, int y, int sel_start, int sel_end)
{
assert(hdc,"txt_rend::draw_buffer, invalid hdc");
if(wstr == L"")
return;
HDC hdc_mem = ::CreateCompatibleDC(hdc);
assert(hdc_mem,"txt_rend::draw_buffer, failed to create hdc");
HBITMAP hdc_bmp = ::CreateCompatibleBitmap(hdc,script_width(hdc,wstr),script_height(hdc,wstr));
::SelectObject(hdc_mem,hdc_bmp);
assert(hdc_mem,"txt_rend::draw_buffer, failed to create hdc");
draw_impl(hdc_mem,wstr,0,0,sel_start,sel_end);
::BitBlt(hdc,x,y,script_width(hdc,wstr),script_height(hdc,wstr),hdc_mem,0,0,SRCCOPY);
::DeleteObject(hdc_bmp);
}
olivthill
October 12th, 2008, 02:37 PM
I don't know if this is useful or not, but I have the habit of deleting the temporary DC. I am also following Petzold's examples when he memorizes the old selected object and select it back at the end:[...]
HBITMAP hdc_bmp_old = (HBITMAP )(::SelectObject(hdc_mem,hdc_bmp));
[...]
::BitBlt([...]);
::SelectObject(hdc_mem,hdc_bmp_old);
::DeleteObject(hdc_bmp);
::DeleteDC(hdc_mem);
}
g3RC4n
October 12th, 2008, 05:30 PM
after all that it was that i forgot the delete hdc_mem
::DeleteObject(hdc_mem);
thanks for your help anyway
Marc G
October 14th, 2008, 09:27 AM
I am also following Petzold's examples when he memorizes the old selected object and select it back at the end:
Actually, that's not really something optional, reselecting the original object is a requirement as per the MSDN:
This function returns the previously selected object of the specified type. An application should always replace a new object with the original, default object after it has finished drawing with the new object. :wave:
g3RC4n
October 15th, 2008, 04:37 PM
Actually, that's not really something optional, reselecting the original object is a requirement as per the MSDN:
:wave:
thats why i made this
class temp_select
{
public:
temp_select(HDC hdc, HGDIOBJ obj)
:
m_hdc(hdc)
{
old_obj = ::SelectObject(m_hdc,obj);
}
~temp_select()
{
::SelectObject(m_hdc,old_obj);
}
private:
HDC m_hdc;
HGDIOBJ old_obj;
};
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.