Results 1 to 7 of 7

Thread: [2005] GDI+ Out of memory

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    [2005] GDI+ Out of memory

    I've created a custom control that is a level meter (a bar that shows audio levels visually). So this gets refreshed about every 200 milliseconds. I am running into an issue when the app runs for somewhere between 12 - 16 hours one of the 4 meters that are displayed on the screen goes white with the red X through it. A message pops up saying OutOfMemory error. Does anyone have an idea why this would be happening? The system memory is always in normal range when this happens. I also added try catch to everything and if an error was thrown it would write the info in a log. This doesn't happen though.

    I first draw this control to a bitmap, then draw the bitmap to the control. I override the onpaint event and use the onpaint eventArg to draw to the control.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] GDI+ Out of memory

    Do you Dispose each of these Bitmaps after you've finished with it? I would think that the Bitmap would be unnecessary anyway. Wouldn't it be better to just draw directly to the control surface using GDI+?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] GDI+ Out of memory

    When I draw directly to the surface I get flashing, because the control is refreshed so often. The bitmap is declared at the class level and is only disposed and recreated if the control is resized. This is a manual way of doing DoubleBuffering. But everything else that can be disposed is disposed.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2005] GDI+ Out of memory

    You are right to use a bitmap as a backbuffer.

    GDI+ sometimes gives OOM errors for non-memory related problems, for example tryin to create a 256 color palletised bitmap. Unless you post some code, we aren't going to know what the problem is
    I don't live here any more.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] GDI+ Out of memory

    Here is the code for the drawing
    VB Code:
    1. If _BackBuffer Is Nothing Then
    2.                 _BackBuffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
    3.             End If
    4.  
    5.             Dim x As Integer
    6.             Dim y As Integer
    7.             Dim rect As Rectangle
    8.             Dim cnt As Integer = 0
    9.             Dim g As Graphics = Graphics.FromImage(_BackBuffer)
    10.             Dim p As Pen = Nothing
    11.             Dim b As LinearGradientBrush = Nothing
    12.             Dim mAv As Integer  'is the actual value taking in consideration the number of led lights compared to the maximum
    13.             Dim Hav As Integer  'is the most recent High Value it hovers for a few seconds then fades until another value is greater
    14.  
    15.  
    16.             g.Clear(Color.White)
    17.             g.SmoothingMode = SmoothingMode.AntiAlias
    18.             Dim c1 As Color
    19.             Dim c2 As Color
    20.  
    21.             If _Orientation = Windows.Forms.Orientation.Vertical Then
    22.                 x = 0
    23.                 y = Me.Height - H - 1
    24.             Else
    25.                 x = 0
    26.                 y = 0
    27.             End If
    28.  
    29.  
    30.             mAv = CInt((AV * _Value) / _Max)
    31.             Hav = CInt((AV * _HighValue) / _Max)
    32.  
    33.             Do Until cnt >= AV
    34.                 rect = New Rectangle(x, y, W, H)
    35.                 If cnt <= AV / 2 Then
    36.                     If cnt <= mAv AndAlso cnt <> Hav Then
    37.                         b = New LinearGradientBrush(rect, Color.LightGreen, Color.LightGreen, LinearGradientMode.Vertical)
    38.                         p = New Pen(Color.Green, 1)
    39.                     ElseIf cnt > mAv AndAlso cnt <> Hav Then
    40.                         c1 = ColorChanger.SetBrightness(Color.Green, 0.1)
    41.                         c1 = ColorChanger.SetSaturation(c1, 0.2)
    42.  
    43.                         c2 = ColorChanger.SetBrightness(Color.Green, 0.2)
    44.                         c2 = ColorChanger.SetSaturation(c2, 0.3)
    45.  
    46.                         b = New LinearGradientBrush(rect, c1, c2, LinearGradientMode.Vertical)
    47.                         p = New Pen(c1, 1)
    48.                     ElseIf cnt = Hav Then
    49.                         b = New LinearGradientBrush(rect, Color.LightBlue, Color.LightBlue, LinearGradientMode.Vertical)
    50.                         p = New Pen(Color.DarkBlue)
    51.                     End If
    52.                 ElseIf cnt > AV / 2 And cnt <= (AV / 4) * 3 Then
    53.                     If cnt <= mAv AndAlso cnt <> Hav Then
    54.                         b = New LinearGradientBrush(rect, ColorChanger.ModifyBrightness(Color.Yellow, 1.3), ColorChanger.ModifyBrightness(Color.Yellow, 1.3), LinearGradientMode.Vertical)
    55.                         p = New Pen(ColorChanger.ModifyBrightness(Color.Yellow, 0.5), 1)
    56.                     ElseIf cnt > mAv AndAlso cnt <> Hav Then
    57.                         c1 = ColorChanger.SetBrightness(Color.Yellow, 0.3)
    58.                         c1 = ColorChanger.SetSaturation(c1, 0.4)
    59.  
    60.                         c2 = ColorChanger.SetBrightness(Color.Yellow, 0.4)
    61.                         c2 = ColorChanger.SetSaturation(c2, 0.5)
    62.  
    63.                         b = New LinearGradientBrush(rect, c1, c2, LinearGradientMode.Vertical)
    64.                         p = New Pen(c1, 1)
    65.                     ElseIf cnt = Hav Then
    66.                         b = New LinearGradientBrush(rect, Color.LightBlue, Color.LightBlue, LinearGradientMode.Vertical)
    67.                         p = New Pen(Color.DarkBlue)
    68.                     End If
    69.  
    70.                 ElseIf cnt > (AV / 4) * 3 Then
    71.                     If cnt <= mAv AndAlso cnt <> Hav Then
    72.                         b = New LinearGradientBrush(rect, Color.Red, Color.Red, LinearGradientMode.Vertical)
    73.                         p = New Pen(ColorChanger.ModifyBrightness(Color.DarkRed, 0.2), 1)
    74.                     ElseIf cnt > mAv AndAlso cnt <> Hav Then
    75.                         c1 = ColorChanger.SetBrightness(Color.DarkRed, 0.3)
    76.                         c1 = ColorChanger.SetSaturation(c1, 0.4)
    77.  
    78.                         c2 = ColorChanger.SetBrightness(Color.DarkRed, 0.4)
    79.                         c2 = ColorChanger.SetSaturation(c2, 0.5)
    80.  
    81.                         p = New Pen(c1, 1)
    82.                         b = New LinearGradientBrush(rect, c1, c2, LinearGradientMode.Vertical)
    83.                     ElseIf cnt = Hav Then
    84.                         b = New LinearGradientBrush(rect, Color.LightBlue, Color.LightBlue, LinearGradientMode.Vertical)
    85.                         p = New Pen(Color.LightBlue)
    86.                     End If
    87.  
    88.                 End If
    89.  
    90.  
    91.                 g.FillRectangle(b, rect)
    92.                 g.DrawRectangle(p, rect)
    93.  
    94.                 If _Orientation = Windows.Forms.Orientation.Vertical Then
    95.                     y -= H
    96.                 ElseIf _Orientation = Windows.Forms.Orientation.Horizontal Then
    97.                     x += W
    98.                 End If
    99.                 cnt += 1
    100.                 rect = Nothing
    101.                 p.Dispose()
    102.                 b.Dispose()
    103.             Loop
    104.             g.Dispose()
    105.             e.DrawImageUnscaled(_BackBuffer, 0, 0)
    106.             e.Dispose()     'originally I didn't dispose of this manually.  But I added it, and still got the error

    Just a few notes:
    w, and h are declared as integers at the class level. I have them set in the sizeChange event.

    ColorChanger is a color manipulation class that I got from Bob Powels website.

    AV is declared as an integer at the class level. This is _value converted to work with however many rectangles there actually are.

    e is the graphics from th onpain argument passed to this method.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2005] GDI+ Out of memory

    Does the error occur at the same line each time?
    I don't live here any more.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] GDI+ Out of memory

    That's part of the problem. It doesn't give me a line number. This doesn't happen on my development machine(it has other issues and won't run long enough). So it is on a test machine that I have a debug install on (debug builds and pdb files) Other errors I do get a line, but not this one. I also have this entire method in a try catch, that is supposed to write to a to a file the error and display a messagebox. But it doesn't do that.

    I've attached a picture of the error it give me
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

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