Solved

Checking code for memory leak

Posted on 2014-10-16
4
303 Views
Last Modified: 2014-10-20
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

0
Comment
Question by:JedNebula
  • 2
  • 2
4 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40384372
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
 
LVL 1

Author Comment

by:JedNebula
ID: 40386213
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40391750
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
 
LVL 1

Author Closing Comment

by:JedNebula
ID: 40391767
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question