?
Solved

Checking code for memory leak

Posted on 2014-10-16
4
Medium Priority
?
356 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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