Checking code for memory leak

I have a form with a rich text box on that I use in my VB.NET app to output status updates to the user. I have modified the AddStatusText routine over the years to check for invoke as it is often called from different threads than the UI one.

In my latest build, the app has been throwing 'System.OutOfMemoryException'

I am trying to track down where this is from recently. The stack trace would indicate that it is to do with my Rich Text Box - the only one I have in the app, although this could just be the victim I suppose, not the cause.

Below I have posted the code I use to update the RTB and also the error I am getting.

If you spot any objects I should be disposing or any potential leaks, please let me know.

Application Unhandled Exception Event Log.

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.IO.MemoryStream.set_Capacity(Int32 value)
at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.Windows.Forms.RichTextBox.EditStreamProc(IntPtr dwCookie, IntPtr buf, Int32 cb, Int32& transferred)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Public Class frmMDIStatus

    Private Delegate Sub OutputMessageDelegate(ByVal strText As String, ByVal Type As StatusType, ByVal Colour As System.Drawing.Color)

    Private Sub frmMDIStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.rtxtStatus.Dock = DockStyle.Fill
    End Sub

    Public Enum StatusType
        Header = 1
        SmallText = 2
    End Enum

    Sub AddStatusText(ByVal strText As String, ByVal Type As StatusType, ByVal Colour As System.Drawing.Color)

        If Me.InvokeRequired Then
            ' if operating on a thread, invoke a delegate 
            ' on the UI thread. 
            Dim omd As OutputMessageDelegate = _
            New OutputMessageDelegate(AddressOf AddStatusText)
            Dim arx As IAsyncResult = Me.BeginInvoke( _
              omd, New Object() {strText, Type, Colour})
            'Application.DoEvents()
            Me.EndInvoke(arx)

            Return
        End If

        Dim stdFont As Font = Nothing

        Try
            With Me.rtxtStatus
                .SuspendLayout()
                Select Case Type
                    Case StatusType.Header
                        stdFont = New Font("Tahoma", 9, FontStyle.Bold, GraphicsUnit.Point)
                    Case StatusType.SmallText
                        stdFont = New Font("Tahoma", 9, FontStyle.Regular, GraphicsUnit.Point)
                End Select

                .SelectionColor = Colour
                .SelectionFont = stdFont

                .SelectionStart = Len(.Text)
                .SelectedText = (strText & vbNewLine)
                .ScrollToCaret()

            End With

        Catch ex As Exception
            Throw
        Finally
            If stdFont IsNot Nothing Then stdFont.Dispose()
            Me.rtxtStatus.ResumeLayout()
        End Try


    End Sub
End Class

Open in new window

LVL 1
JedNebulaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Do you know how much memory your application is using when this error is thrown? Its either too much text going in rich text box or the rest of application leaking memory.
0
JedNebulaAuthor Commented:
A customer rang this morning saying the crash was on the screen at the time. I dialled in and task manager was reporting that the memory usage was 1.5 GB!

So I think it's safe to say the Rich Text box is the victim, not the cause.

Now I have to try and find a way of narrowing the cause down. Unfortunately, the purpose of the app is to run off various 'tasks' at timed intervals (usually every few minutes throughout the day) - like SQL backup, copying data from here to there, uploading data to the web, etc.

Could be one or more of them that is the cause.

I'll probably find I have stupidly not disposed something I should have done.

With that amount of memory being used up, is there a tool I can use, to see what is taking up that memory?

Sorry, I am such a noob at memory management.
0
CodeCruiserCommented:
With repetitive tasks, it is really easy to leak memory if you are not carefully getting rid of objects. You need to review your code and make sure you are disposing all objects. You may have to explicitly call garbage collector. If still an issue, run your app locally and monitor memory usage by focusing on one task (that you app does) at a time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JedNebulaAuthor Commented:
Sorry, forgot to close this last week.

I went through the code as you suggested and found some objects that were not being disposed.

No more leak.

Many thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.