Exception object causing Memory Leak?

B

bw

I have a basic custom collection that throws a new
exception if the item(key) is not found in the collection.
This is used as part of a calling function. It all works
correctly, the problem (discovered using a memory
profiler) is that the base exception being thrown in the
collection is not being disposed of.

I understand about the GC etc etc. It appears that
something is hanging on to a reference to System.Exception
and a corresponding System.SByte[] (the exception
string??).

Can anyone tell me what is going on here? After a period
of time, this builds up to a significant amount of memory
being leaked!

I have heared of the problem in V1.0 of the framework,
where a similar problem occurs if you throw a new
exception from within a catch block. Maybe this is
similar? I am using V1.1 of the framework.

THANKS!

Sample code :


Public Class MYCollection : Inherits
Collections.DictionaryBase

...

Friend Function Item(ByVal Key As String) As Object
Item = MyBase.Dictionary.Item(Key)
If Item Is Nothing Then
Throw New System.Exception("This item does not
exist in the collection.")
End If
End Function

...

End Class


Friend Function funcGet(ByRef DocumentIndex As Integer) As
MyCLS

...

Try

funcGet = CType(MYCollection.Item("K" &
DocumentIndex), MyCLS)
If funcGet Is Nothing Then
Return Nothing
End If

Return funcGet

Catch

Return Nothing
End Try

End Function
 
J

Josh Moody [MSFT]

So I wrote a simple little function to test this, and it doesn't seem to be
leaking.

Create a webform with two buttons on it (start and stop)

Dim th As Threading.Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
th = New Threading.Thread(AddressOf f)
th.Start()

End Sub
Sub f()
While True
Try
Throw New Exception("Bugger!")
Catch ex As Exception

End Try
End While
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
th.Abort()
End Sub

Open up task manager, start the program, and hit the start button. On my
machine, memory slowly grew for about a minute, then leveled off and
stopped growing. To me this probably means that it's just a timing issue
with the garbage collector or something similar. If after it's leveled off,
you press stop and then start it up again, it does not appear to grow at
all.

Do you have any other repros for this that would show this?

Josh Moody
VSU Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
Content-Class: urn:content-classes:message
From: "bw" <[email protected]>
Sender: "bw" <[email protected]>
Subject: Exception object causing Memory Leak?
Date: Fri, 10 Oct 2003 01:20:46 -0700
Lines: 65
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcOPB2fHmHL78oB1TPeZcZLqKSYgcA==
Newsgroups: microsoft.public.dotnet.languages.vb
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:145594
NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I have a basic custom collection that throws a new
exception if the item(key) is not found in the collection.
This is used as part of a calling function. It all works
correctly, the problem (discovered using a memory
profiler) is that the base exception being thrown in the
collection is not being disposed of.

I understand about the GC etc etc. It appears that
something is hanging on to a reference to System.Exception
and a corresponding System.SByte[] (the exception
string??).

Can anyone tell me what is going on here? After a period
of time, this builds up to a significant amount of memory
being leaked!

I have heared of the problem in V1.0 of the framework,
where a similar problem occurs if you throw a new
exception from within a catch block. Maybe this is
similar? I am using V1.1 of the framework.

THANKS!

Sample code :


Public Class MYCollection : Inherits
Collections.DictionaryBase

...

Friend Function Item(ByVal Key As String) As Object
Item = MyBase.Dictionary.Item(Key)
If Item Is Nothing Then
Throw New System.Exception("This item does not
exist in the collection.")
End If
End Function

...

End Class


Friend Function funcGet(ByRef DocumentIndex As Integer) As
MyCLS

...

Try

funcGet = CType(MYCollection.Item("K" &
DocumentIndex), MyCLS)
If funcGet Is Nothing Then
Return Nothing
End If

Return funcGet

Catch

Return Nothing
End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top