Solved

Checking code for memory leak

Posted on 2014-10-16
4
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 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