Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. LoadImage failure?

LoadImage failure?

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsperformancequestion
12 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Llasus
    wrote on last edited by
    #1

    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!

    H 1 Reply Last reply
    0
    • L Llasus

      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!

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Can you show your code?

      L 1 Reply Last reply
      0
      • H Hamid Taebi

        Can you show your code?

        L Offline
        L Offline
        Llasus
        wrote on last edited by
        #3

        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); }

        H N J 3 Replies Last reply
        0
        • L Llasus

          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); }

          H Offline
          H Offline
          Hamid Taebi
          wrote on last edited by
          #4

          What is gOtherClass.gDisplayPanelHandle?

          L 1 Reply Last reply
          0
          • H Hamid Taebi

            What is gOtherClass.gDisplayPanelHandle?

            L Offline
            L Offline
            Llasus
            wrote on last edited by
            #5

            its a class which contains all the shared variables between other characters. I have noticed also that after around 100 times of loading a bitmap, the application's performance is starting to decline.

            1 Reply Last reply
            0
            • L Llasus

              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); }

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #6

              Llasus wrote:

              gOtherClass.gDisplayPanelHandle

              Do you destroy the handle after every display?

              nave [OpenedFileFinder]

              L 1 Reply Last reply
              0
              • L Llasus

                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); }

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                When calling SelectObject, you need to store the Pointer to the old GdiObject 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

                L 1 Reply Last reply
                0
                • N Naveen

                  Llasus wrote:

                  gOtherClass.gDisplayPanelHandle

                  Do you destroy the handle after every display?

                  nave [OpenedFileFinder]

                  L Offline
                  L Offline
                  Llasus
                  wrote on last edited by
                  #8

                  yes. I made sure that it destroys the object before loading the next image.

                  1 Reply Last reply
                  0
                  • J jhwurmbach

                    When calling SelectObject, you need to store the Pointer to the old GdiObject 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

                    L Offline
                    L Offline
                    Llasus
                    wrote on last edited by
                    #9

                    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.

                    J 1 Reply Last reply
                    0
                    • L Llasus

                      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.

                      J Offline
                      J Offline
                      jhwurmbach
                      wrote on last edited by
                      #10

                      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"

                      L 2 Replies Last reply
                      0
                      • J jhwurmbach

                        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"

                        L Offline
                        L Offline
                        Llasus
                        wrote on last edited by
                        #11

                        I see. thank you very much for the help! I will try to test this tomorrow. Again, thank you very much for the time and help!

                        1 Reply Last reply
                        0
                        • J jhwurmbach

                          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"

                          L Offline
                          L Offline
                          Llasus
                          wrote on last edited by
                          #12

                          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. :)

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups