Out of memory
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Out of memory

  1. #1
    Join Date
    Mar 2011

    Out of memory

    Hello guys,
    New riddle for you,

    I am using a method to rotate images in a form.

    private: System::Drawing::Image^ rotateImage(Image^ b, float angle)
                //create a new empty bitmap to hold rotated image
    			Bitmap^ returnBitmap = gcnew Bitmap(b->Width, b->Height);
                //make a graphics object from the empty bitmap
                Graphics^ g = Graphics::FromImage(returnBitmap);
                //move rotation point to center of image
    			g->TranslateTransform((float)b->Width / 2, (float)b->Height / 2);
                //move image back
                g->TranslateTransform(-(float)b->Width / 2, -(float)b->Height / 2);
                //draw passed in image onto graphics object
    			g->DrawImage(b, 0, 0);
    			return returnBitmap;
    and it goes good, but sometimes after some time running the program I get Out of Memory error in line
    g->DrawImage(b, 0, 0);
    Is this a disposal issue ? do you have any idea how to fix it ?

    Thanks and best regards,

  2. #2
    Join Date
    Jul 2002

    Re: Out of memory

    Graphics is disposable class and should be deleted after using:
    delete g;

  3. #3
    Join Date
    Jun 2010

    Re: Out of memory

    Alex F's objection emphasizes that your program may be suffering from a memory or other resource leak. Did you try to observe memory consumption of the program using Task Manager or Sysinternals' Process Explorer? If you don't observe a steady increase in memory consumption, you should have a look at the GDI object count and handle count.

    Also, the methods from the System::Drawing namespace are more or less known for throwing an OutOfMemoryException in case the underlying GDI+ function encounters any problem in handling the bitmap format. (This has for instance been discussed in http://www.codeguru.com/forum/showthread.php?t=513030.) Perhaps they may even do that if it's not documented on MSDN (which would be the case for Graphics::DrawImage()). While I'm not quite sure now whether that's true for that sort of GDI+ problems as well, in general the information carried by the .NET exception objects often is quite helpful, so I'd say it's definitely worth a try to have a closer look at the exception object you catch. In case it's a GDI+ problem, the exception being thrown is likely to be correlated to the use of certain (types of) bitmap objects and/or rotation angles.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Mar 2011

    Re: Out of memory

    Thanks for your answer,

    It is not very easy to reproduce the exception (since as I wrote it appears in unexpected moments). However I am now disposing the Graphics, and also I have rewrite a bit my source code, because i was kind of exploiting this method .

    Now I don't get this out of memory error anymore. When I will have some spare time I will look into what You have written Eri.

    Thanks for your help and time guys !
    Best regards,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Azure Activities Information Page

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


HTML5 Development Center