LoadImage failure?
-
Hello and good day. I currently have a problem with LoadImage (i think) in my application. The application reads a file, changes it into bitmap format, uses LoadImage to display a file, then uses StretchBlt to resize/rotate it. The user selects the file to load and display. Now the problem is, I tried to do a stress test for it and the LoadImage fails after around 100 tries of loading a 2000x2000 bmp file. I checked the HBITMAP handler which receives the data from the LoadImage and it contains NULL at that time. I still am not sure if the LoadImage is really the problem, I think it may be all the memory allocations I made but I checked them again and again and they were certainly released when a new image is loaded. Can somebody please advise me and help me about this problem? Thank you!
-
Hello and good day. I currently have a problem with LoadImage (i think) in my application. The application reads a file, changes it into bitmap format, uses LoadImage to display a file, then uses StretchBlt to resize/rotate it. The user selects the file to load and display. Now the problem is, I tried to do a stress test for it and the LoadImage fails after around 100 tries of loading a 2000x2000 bmp file. I checked the HBITMAP handler which receives the data from the LoadImage and it contains NULL at that time. I still am not sure if the LoadImage is really the problem, I think it may be all the memory allocations I made but I checked them again and again and they were certainly released when a new image is loaded. Can somebody please advise me and help me about this problem? Thank you!
Can you show your code?
-
Can you show your code?
The code is actually too large to paste in here. I can only paste the function which displays has the LoadImage part.
void CDlg::DisplayImage() { //then use LoadImage and attach it to a BITMAP handle gOtherClass.gDisplayPanelHandle = (HBITMAP)::LoadImage ( 0, gMapPath, IMAGE_BITMAP, gSourceSize.cx, gSourceSize.cy, LR_DEFAULTCOLOR | LR_LOADFROMFILE ); //check if GlobalMap.bmp was loaded if(gOtherClass.gDisplayPanelHandle == NULL) { AfxMessageBox("Error!\nUnable to load Map File"); return; } //Attach the bitmap handle to the CDC for display gCDC.SelectObject(gOtherClass.gDisplayPanelHandle); //reverse image and stretch it to display correctly gCDC.StretchBlt ( 0, 0, gSourceSize.cx, gSourceSize.cy, &gCDC, 0, gSourceSize.cy - 1, //added - 1 to display image properly gSourceSize.cx, -gSourceSize.cy, SRCCOPY ); Invalidate(FALSE); }
-
The code is actually too large to paste in here. I can only paste the function which displays has the LoadImage part.
void CDlg::DisplayImage() { //then use LoadImage and attach it to a BITMAP handle gOtherClass.gDisplayPanelHandle = (HBITMAP)::LoadImage ( 0, gMapPath, IMAGE_BITMAP, gSourceSize.cx, gSourceSize.cy, LR_DEFAULTCOLOR | LR_LOADFROMFILE ); //check if GlobalMap.bmp was loaded if(gOtherClass.gDisplayPanelHandle == NULL) { AfxMessageBox("Error!\nUnable to load Map File"); return; } //Attach the bitmap handle to the CDC for display gCDC.SelectObject(gOtherClass.gDisplayPanelHandle); //reverse image and stretch it to display correctly gCDC.StretchBlt ( 0, 0, gSourceSize.cx, gSourceSize.cy, &gCDC, 0, gSourceSize.cy - 1, //added - 1 to display image properly gSourceSize.cx, -gSourceSize.cy, SRCCOPY ); Invalidate(FALSE); }
What is gOtherClass.gDisplayPanelHandle?
-
What is gOtherClass.gDisplayPanelHandle?
-
The code is actually too large to paste in here. I can only paste the function which displays has the LoadImage part.
void CDlg::DisplayImage() { //then use LoadImage and attach it to a BITMAP handle gOtherClass.gDisplayPanelHandle = (HBITMAP)::LoadImage ( 0, gMapPath, IMAGE_BITMAP, gSourceSize.cx, gSourceSize.cy, LR_DEFAULTCOLOR | LR_LOADFROMFILE ); //check if GlobalMap.bmp was loaded if(gOtherClass.gDisplayPanelHandle == NULL) { AfxMessageBox("Error!\nUnable to load Map File"); return; } //Attach the bitmap handle to the CDC for display gCDC.SelectObject(gOtherClass.gDisplayPanelHandle); //reverse image and stretch it to display correctly gCDC.StretchBlt ( 0, 0, gSourceSize.cx, gSourceSize.cy, &gCDC, 0, gSourceSize.cy - 1, //added - 1 to display image properly gSourceSize.cx, -gSourceSize.cy, SRCCOPY ); Invalidate(FALSE); }
Llasus wrote:
gOtherClass.gDisplayPanelHandle
Do you destroy the handle after every display?
nave [OpenedFileFinder]
-
The code is actually too large to paste in here. I can only paste the function which displays has the LoadImage part.
void CDlg::DisplayImage() { //then use LoadImage and attach it to a BITMAP handle gOtherClass.gDisplayPanelHandle = (HBITMAP)::LoadImage ( 0, gMapPath, IMAGE_BITMAP, gSourceSize.cx, gSourceSize.cy, LR_DEFAULTCOLOR | LR_LOADFROMFILE ); //check if GlobalMap.bmp was loaded if(gOtherClass.gDisplayPanelHandle == NULL) { AfxMessageBox("Error!\nUnable to load Map File"); return; } //Attach the bitmap handle to the CDC for display gCDC.SelectObject(gOtherClass.gDisplayPanelHandle); //reverse image and stretch it to display correctly gCDC.StretchBlt ( 0, 0, gSourceSize.cx, gSourceSize.cy, &gCDC, 0, gSourceSize.cy - 1, //added - 1 to display image properly gSourceSize.cx, -gSourceSize.cy, SRCCOPY ); Invalidate(FALSE); }
When calling
SelectObject
, you need to store the Pointer to the oldGdiObject
you get from the call and re-SelectObject
it after finishing work. You are producing a so called GDI-Resource-Leak. In the ProcessExplorer, you cann swith on display of used GDI-Objects.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
Llasus wrote:
gOtherClass.gDisplayPanelHandle
Do you destroy the handle after every display?
nave [OpenedFileFinder]
-
When calling
SelectObject
, you need to store the Pointer to the oldGdiObject
you get from the call and re-SelectObject
it after finishing work. You are producing a so called GDI-Resource-Leak. In the ProcessExplorer, you cann swith on display of used GDI-Objects.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening wordsThank you for the help. I will try this tomorrow. I have not encountered this GDI-Resource-Leak though. Sorry but could you please explain further what you mean by re-SelectObject after finishing the work and why I need it? Usually when loading the next image I just use the DeleteObject() to somehow release the object for next use.
-
Thank you for the help. I will try this tomorrow. I have not encountered this GDI-Resource-Leak though. Sorry but could you please explain further what you mean by re-SelectObject after finishing the work and why I need it? Usually when loading the next image I just use the DeleteObject() to somehow release the object for next use.
Llasus wrote:
could you please explain further what you mean by re-SelectObject after finishing the work and why I need it?
What I mean is this:
CBitmap m_bmpSnapshot = ...
CBitmap* pOldBmp = dcSnapshot.SelectObject(&m_bmpSnapshot);
dcSnapshot.BitBlt(0, 0, width, height, &dc, 0, 0, SRCCOPY);
dcSnapshot.SelectObject(pOldBmp);
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
Llasus wrote:
could you please explain further what you mean by re-SelectObject after finishing the work and why I need it?
What I mean is this:
CBitmap m_bmpSnapshot = ...
CBitmap* pOldBmp = dcSnapshot.SelectObject(&m_bmpSnapshot);
dcSnapshot.BitBlt(0, 0, width, height, &dc, 0, 0, SRCCOPY);
dcSnapshot.SelectObject(pOldBmp);
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency" -
Llasus wrote:
could you please explain further what you mean by re-SelectObject after finishing the work and why I need it?
What I mean is this:
CBitmap m_bmpSnapshot = ...
CBitmap* pOldBmp = dcSnapshot.SelectObject(&m_bmpSnapshot);
dcSnapshot.BitBlt(0, 0, width, height, &dc, 0, 0, SRCCOPY);
dcSnapshot.SelectObject(pOldBmp);
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency"It worked! Thank you so much for your help. I was never aware of the possibility that GDI may leak if you don't save and re-select the pointer that SelectObject returned. I did past applications though without doing that. Though not as much on loading in images like this one. Again, thank you to all that helped specially jhwurmbach. :)