Solved

Checking code for memory leak

Posted on 2014-10-16
4
332 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 calling COM DLL written in Visual Studio 2003 6 54
Best book for Internet security 4 48
Open telerik datetime picker programmatically 13 41
search by any word in column 5 28
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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