[RESOLVED] VS 2017 GDI Memory Leak
I have the following as member variables of a UserControl for my football simulation game.
Code:
Private m_Logo() As Image
Private m_imPassing(5) As Image
Private m_imRushing(5) As Image
Private m_imReceiving(5) As Image
Private m_imTackles(5) As Image
This UserControl simulates games and updates the screen with info as each game finishes. m_imRushing and the other variables listed above are the team logos associated with the stat leaders in each category (ranked 1 through 5). Drawing these images in Paint is what's causing my error (which comes in the form of "Parameter is not Valid").
Here's more background information: I use m_Logo() to store the images of each team in the UCs Load event where m_League is an object my own class (CLeague) that stores all league information.
Code:
ReDim m_Logo(m_League.NumberOfTeams - 1)
For x = 0 To m_League.NumberOfTeams - 1
m_Logo(x) = ResizeLogo(m_League.Team(x).Logo)
Next
My ResizeLogo function takes the stored large logo and shrinks it so it fits nicely next to a player's name:
Code:
Private Function ResizeLogo(ByVal OriginalLogo As Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap(24, 24)
'' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
'' Copy the source image into the destination bitmap.
gr_dest.DrawImage(OriginalLogo, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
bm_dest.Dispose()
gr_dest.Dispose()
Return bm_dest
End Function
When the user clicks a button "Sim All Games" a loop is started to simulate each game of the week
Code:
For x = 0 To GameCount - 1
'Tons of processing code not relevant
m_imPassing(1) = m_Logo(m_League.Team(x).TeamIndex)
m_imRushing(1) = m_Logo(m_League.Team(x).TeamIndex)
'etc...
next
So my member variables are set. I call Invalidate() after every game is simmed which is supposed to update the Logos. After about a minute of successful execution I receive the dreaded "Parameter not valid" error for the DrawImage call.
Code:
Private Sub pnWeeklyLeaders_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnWeeklyLeaders.Paint
Dim g As Graphics = e.Graphics
Dim y As Byte
Dim LogoSpace As Byte = 27
For y = 0 To 4
Dim LogoLocation As New Point(37, 82 + (y * LogoSpace))
If m_pbPassing(y) IsNot Nothing Then g.DrawImage(m_imPassing(y), LogoLocation)
Next
end sub
There's something about using the UCs member variables that's causing memory usage to build up. When I take out the logos drawing routine I can run through almost the entire week's worth of games before the error shows up (there may be another lesser leak as well, but one problems at a time for this post) Where is this memory buildup and how should I dispose it? I can't dispose m_imPassing and the other variables because I reuse them do to their scope. There are no IDisposable objects to dispose either.
Thanks!
neef
Re: VS 2017 GDI Memory Leak
Your ResizeLogo method doesn't really make sense. You create a Bitmap, dispose it and then return it. You presumably then use that Bitmap later, so there's no wonder you have issues. You are supposed to dispose a Bitmap - or any object - AFTER you have finished using it. Don't dispose it first.
Re: VS 2017 GDI Memory Leak
Also, it's not a memory leak. A memory leak is a specific thing. There's no memory being leaked here.
Re: VS 2017 GDI Memory Leak
Quote:
Originally Posted by
jmcilhinney
Your ResizeLogo method doesn't really make sense. You create a Bitmap, dispose it and then return it. You presumably then use that Bitmap later, so there's no wonder you have issues. You are supposed to dispose a Bitmap - or any object - AFTER you have finished using it. Don't dispose it first.
bm_dest is commented out in my actual code but I erased the apostrophe when I transferring the text to this thread - my bad. I still have the error.
Re: VS 2017 GDI Memory Leak
Quote:
Originally Posted by
jmcilhinney
Also, it's not a memory leak. A memory leak is a specific thing. There's no memory being leaked here.
What could cause a "Parameter is not Valid Error" in the paint event then? The error pops up after the code works for over a minute. When I take out all code relevant to drawing my logos I don't get the eroor at all. This error is so frustrating because it's so vague!
Re: VS 2017 GDI Memory Leak
If you want help with code that's not working, I suggest showing us the code that's not working.
Re: VS 2017 GDI Memory Leak
It's working. It turned out it was bm_dest.dispose like you correctly identified. That line wasn't commented out as I wrote in my last post. Just carelessness on my part.
I have another issue but that'll wait for tomorrow if I can't fix it. Until then, thanks JMC.