Results 1 to 15 of 15

Thread: [RESOLVED] Robust response to Out Of Memory Exception

Threaded View

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Resolved [RESOLVED] Robust response to Out Of Memory Exception

    I've noticed that GDI+ and other graphics can crash when asked to deal with very large images, and the crash doesn't always occur where you'd expect it. I am getting this when testing the slide show program I posted to the code bank yesterday. This is the code concerned:

    vb.net Code:
    1. Private Sub animTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles animTimer.Tick
    2.         If infos.Count = 0 Then Exit Sub
    3.         ticks = (ticks + 1) Mod 200
    4.         Select Case ticks
    5.             Case 0
    6.                'phase 1: Pic2 is shown, Pic1 is hidden. Start loading pic1.
    7.                 picIndex = (picIndex + 1) Mod infos.Count
    8.                 Pic1.Load(infos(picIndex).FullName)
    9.             Case 50 To 99
    10.                 'phase 2: Start fading pic2
    11.                 Try
    12.                     PicForm2.Opacity -= 0.02
    13.                 Catch Ex As Exception
    14.                     MessageBox.Show(Ex.Message)
    15.                 End Try
    16.                 If ticks = 65 Then Me.Text = "Easy Slide Show: " & infos(picIndex).Name
    17.             Case 100
    18.                 'phase 3: Pic1 is shown. Start loading Pic2
    19.                 picIndex = (picIndex + 1) Mod infos.Count
    20.                 Pic2.Load(infos(picIndex).FullName)
    21.             Case 150 To 199
    22.                 'phase 4: Start Unfading Pic2
    23.                 PicForm2.Opacity += 0.02
    24.                 If ticks = 165 Then Me.Text = "Easy Slide Show: " & infos(picIndex).Name
    25.         End Select
    26.     End Sub

    The crash only occurs when asked to deal with a 48Mpixel (6000 x 8000) image, and then only when running in the debugger. The picture box loads the image successfully and displays it OK. The Out of Memory exception is not thrown until I try to fade the form by changing its opacity (line 12).

    Unfortunately Try-Catch doesn't work in this case. If it did I could simply skip the image concerned or maybe show it at a lower resolution. But the OOM error is thrown before the exception is caught. The same applies whether I put Try-Catch just around the offending line as above or round the whole loop.

    I realize that a 48 Megapixel image is a lot to ask, certainly in limited memory, and in normal running even that does not fail. But where should I draw the line? Is the only way to set an arbitrary upper size limit to the images to ward off a potential crash? In this application I would prefer just to skip the image if need be. Can someone recommend a robust way to deal with the exception?

    BB
    Last edited by boops boops; Mar 31st, 2010 at 03:23 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width