Solved

GlobalFree crash

Posted on 2006-06-27
20
558 Views
Last Modified: 2013-12-03
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;
0
Comment
Question by:VapiSoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
20 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 16993697
What kind of crash do you get? If it is a SEH exception, try

LONG ExceptionFilter(DWORD dwCause) {

printf("Found a SEH exception 0x%8.8x\n",dwCause);

return 1;
}

__try {

  // 'GlobalFree()'  here

} __except(ExceptionFilter(GetExceptionCode()) {

  // handle here
}
0
 

Author Comment

by:VapiSoft
ID: 16994086
I already have

try
{
 GlobalFree(hDIB);
}
catch(...)
{
}

What is the difference between try and __try ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16994135
>>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.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:VapiSoft
ID: 16994501
Thank you very much.
I will try it.
0
 

Author Comment

by:VapiSoft
ID: 16994638
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 16994664
>>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.
0
 

Author Comment

by:VapiSoft
ID: 16994779
I tried again, it does not get to the ExceptionFilter(DWORD dwCause) either.
Maybe because it is under the debugger?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16994833
Could you post the exact code how you're trying that?
0
 

Author Comment

by:VapiSoft
ID: 16994862
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?
0
 
LVL 86

Expert Comment

by:jkr
ID: 16994978
That's odd, there is no such exception code....
0
 

Author Comment

by:VapiSoft
ID: 16996362
Opps, the error code is 0xc0000005
0
 
LVL 86

Expert Comment

by:jkr
ID: 16996427
Well, that's a plain EXCEPTION_ACCESS_VIOLATION.
0
 

Author Comment

by:VapiSoft
ID: 16996644
What does it mean in relation to GlobalFree(hDIB)?
Does it mean that it is already free?
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 16996798
Good question. How are you allocating that memory?
0
 

Author Comment

by:VapiSoft
ID: 16999783
First I do GlobalAlloc, and then GlobalReAlloc
0
 
LVL 86

Expert Comment

by:jkr
ID: 17004573
Can you post the code where you create 'hDIB'?
0
 

Author Comment

by:VapiSoft
ID: 17004650
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_ZEROINIT);
 if (!hMem) return 0;
 
      hDIB = hMem;
      //printDebugLog("GlobalReAlloc: hDIB=%x",hDIB);

 lpbih = (LPBITMAPINFOHEADER)hDIB;
 int color_table_size=ColorTableSize(lpbih);
 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,(LPBITMAPINFO) &lpbih,DIB_RGB_COLORS,(void **) &start,0,0);
 //memcpy(start,start1,bih.biSizeImage);

 DWORD t2=GetTickCount();
      //DeleteObject(hbmp);
      printDebugLog("GetDIBits: dt=%d",t2-t1);

 return hDIB;
}

 

HDC hdc=GetWindowDC(hwnd);
 HDC memDC   =CreateCompatibleDC(hdc);
 HBITMAP hbmp=CreateCompatibleBitmap(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.cx,msg->pixels);

 SelectObject(memDC,oldBmp);
 DeleteObject(hbmp);
 DeleteObject(memDC);
 ReleaseDC(hwnd, hdc);

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question