VapiSoft
asked on
GlobalFree crash
Once every few days my app crashes inside GlobalFree.
Before I call the function, I check that the memory is OK by IsBadWritePtr.
Is there a way to make sure that it will not crash?
Or is there a way like try{}catch() that will avoid the crash.
Thanks,
Shlomo;
Before I call the function, I check that the memory is OK by IsBadWritePtr.
Is there a way to make sure that it will not crash?
Or is there a way like try{}catch() that will avoid the crash.
Thanks,
Shlomo;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>What is the difference between try and __try ?
'try' will catch ++ exceptions, but *no* SEH exceptions (unless you provide a translator, see http://msdn.microsoft.com/library/en-us/vclib/html/_CRT__set_se_translator.asp) - this applies to access violations also, whereas '__try' will only catch SEH exceptions. They simply belong to two absolutely different exception handling concepts.
'try' will catch ++ exceptions, but *no* SEH exceptions (unless you provide a translator, see http://msdn.microsoft.com/library/en-us/vclib/html/_CRT__set_se_translator.asp) - this applies to access violations also, whereas '__try' will only catch SEH exceptions. They simply belong to two absolutely different exception handling concepts.
ASKER
Thank you very much.
I will try it.
I will try it.
ASKER
I tried it, it doesn't work.
I load it with a debugger, and I changed the hDIB handle.
I expected the __except to catch the "crash", it did not.
I load it with a debugger, and I changed the hDIB handle.
I expected the __except to catch the "crash", it did not.
>>I expected the __except to catch the "crash", it did not.
Wrong assumption. It is not to catch the exception, but to detect the cause. That's the interesting part.
Wrong assumption. It is not to catch the exception, but to detect the cause. That's the interesting part.
ASKER
I tried again, it does not get to the ExceptionFilter(DWORD dwCause) either.
Maybe because it is under the debugger?
Maybe because it is under the debugger?
Could you post the exact code how you're trying that?
ASKER
Opps.
Now I did receive it, and it even helped me not to crash (it did get to the exception handler part).
So first, thank you very much.
Also, the dwCause was 5, what is 5?
Now I did receive it, and it even helped me not to crash (it did get to the exception handler part).
So first, thank you very much.
Also, the dwCause was 5, what is 5?
That's odd, there is no such exception code....
ASKER
Opps, the error code is 0xc0000005
Well, that's a plain EXCEPTION_ACCESS_VIOLATION .
ASKER
What does it mean in relation to GlobalFree(hDIB)?
Does it mean that it is already free?
Does it mean that it is already free?
Good question. How are you allocating that memory?
ASKER
First I do GlobalAlloc, and then GlobalReAlloc
Can you post the code where you create 'hDIB'?
ASKER
HANDLE getPictureHandle(HBITMAP hBitmap, HDC hdc, int &nLines, int &nColumns, LPBYTE &start)
{
BITMAPINFOHEADER bih;
//HPALETTE hPal;
BITMAP Bitmap;
//HDC hDC;
HANDLE hMem;
WORD biBits;
LPBITMAPINFOHEADER lpbih;
GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
int w2=Bitmap.bmWidth;
int h2=Bitmap.bmHeight;
biBits = n_pixels; // Bitmap.bmPlanes * Bitmap.bmBitsPixel;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = w2;
bih.biHeight = h2;
bih.biPlanes = Bitmap.bmPlanes;
bih.biBitCount = n_pixels; // 32; // Bitmap.bmBitsPixel;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 3780;
bih.biYPelsPerMeter = 3780;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
bih.biSizeImage=0;
int dwLen = bih.biSize + ColorTableSize(&bih);
HANDLE hDIB = GlobalAlloc(GPTR, dwLen);
if (!hDIB)
{
return 0;
}
lpbih = (LPBITMAPINFOHEADER)hDIB;
*lpbih = bih;
/// get the actual size
nLines=GetDIBits(hdc,
hBitmap,
(UINT)0,
(UINT)bih.biHeight,
(LPVOID)NULL,
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS);
bih = *lpbih;
if (bih.biSizeImage == 0)
{
if(hDIB)
{
GlobalFreeDIB(hDIB,5);
}
return 0;
}
dwLen = bih.biSize + ColorTableSize(&bih) + bih.biSizeImage;
hMem = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE|GMEM_ZEROINI T);
if (!hMem) return 0;
hDIB = hMem;
//printDebugLog("GlobalReA lloc: hDIB=%x",hDIB);
lpbih = (LPBITMAPINFOHEADER)hDIB;
int color_table_size=ColorTabl eSize(lpbi h);
start=(LPBYTE)lpbih + (WORD)lpbih->biSize + color_table_size;
nColumns=bih.biWidth;
DWORD t1=GetTickCount();
nLines=GetDIBits(hdc,
hBitmap,
(UINT)0,
(UINT)bih.biHeight,
start,
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS);
//CreateDIBSection(hdc,(LP BITMAPINFO ) &lpbih,DIB_RGB_COLORS,(voi d **) &start,0,0);
//memcpy(start,start1,bih. biSizeImag e);
DWORD t2=GetTickCount();
//DeleteObject(hbmp);
printDebugLog("GetDIBits: dt=%d",t2-t1);
return hDIB;
}
HDC hdc=GetWindowDC(hwnd);
HDC memDC =CreateCompatibleDC(hdc);
HBITMAP hbmp=CreateCompatibleBitma p(hdc, wr.cx, wr.cy);
DWORD t3=GetTickCount();
HBITMAP oldBmp=(HBITMAP) SelectObject(memDC,hbmp);
BitBlt(memDC,0,0, wr.cx, wr.cy, hdc, x_offset, y_offset, SRCCOPY);
//oldBmp=(HBITMAP) SelectObject(memDC,hbmp);
DWORD t4=GetTickCount();
msg->hDIB=getPictureHandle (hbmp,hdc, wr.cy,wr.c x,msg->pix els);
SelectObject(memDC,oldBmp) ;
DeleteObject(hbmp);
DeleteObject(memDC);
ReleaseDC(hwnd, hdc);
{
BITMAPINFOHEADER bih;
//HPALETTE hPal;
BITMAP Bitmap;
//HDC hDC;
HANDLE hMem;
WORD biBits;
LPBITMAPINFOHEADER lpbih;
GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
int w2=Bitmap.bmWidth;
int h2=Bitmap.bmHeight;
biBits = n_pixels; // Bitmap.bmPlanes * Bitmap.bmBitsPixel;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = w2;
bih.biHeight = h2;
bih.biPlanes = Bitmap.bmPlanes;
bih.biBitCount = n_pixels; // 32; // Bitmap.bmBitsPixel;
bih.biCompression = 0;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 3780;
bih.biYPelsPerMeter = 3780;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
bih.biSizeImage=0;
int dwLen = bih.biSize + ColorTableSize(&bih);
HANDLE hDIB = GlobalAlloc(GPTR, dwLen);
if (!hDIB)
{
return 0;
}
lpbih = (LPBITMAPINFOHEADER)hDIB;
*lpbih = bih;
/// get the actual size
nLines=GetDIBits(hdc,
hBitmap,
(UINT)0,
(UINT)bih.biHeight,
(LPVOID)NULL,
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS);
bih = *lpbih;
if (bih.biSizeImage == 0)
{
if(hDIB)
{
GlobalFreeDIB(hDIB,5);
}
return 0;
}
dwLen = bih.biSize + ColorTableSize(&bih) + bih.biSizeImage;
hMem = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE|GMEM_ZEROINI
if (!hMem) return 0;
hDIB = hMem;
//printDebugLog("GlobalReA
lpbih = (LPBITMAPINFOHEADER)hDIB;
int color_table_size=ColorTabl
start=(LPBYTE)lpbih + (WORD)lpbih->biSize + color_table_size;
nColumns=bih.biWidth;
DWORD t1=GetTickCount();
nLines=GetDIBits(hdc,
hBitmap,
(UINT)0,
(UINT)bih.biHeight,
start,
(LPBITMAPINFO)lpbih,
DIB_RGB_COLORS);
//CreateDIBSection(hdc,(LP
//memcpy(start,start1,bih.
DWORD t2=GetTickCount();
//DeleteObject(hbmp);
printDebugLog("GetDIBits: dt=%d",t2-t1);
return hDIB;
}
HDC hdc=GetWindowDC(hwnd);
HDC memDC =CreateCompatibleDC(hdc);
HBITMAP hbmp=CreateCompatibleBitma
DWORD t3=GetTickCount();
HBITMAP oldBmp=(HBITMAP) SelectObject(memDC,hbmp);
BitBlt(memDC,0,0, wr.cx, wr.cy, hdc, x_offset, y_offset, SRCCOPY);
//oldBmp=(HBITMAP) SelectObject(memDC,hbmp);
DWORD t4=GetTickCount();
msg->hDIB=getPictureHandle
SelectObject(memDC,oldBmp)
DeleteObject(hbmp);
DeleteObject(memDC);
ReleaseDC(hwnd, hdc);
ASKER
try
{
GlobalFree(hDIB);
}
catch(...)
{
}
What is the difference between try and __try ?