Delegates Memory Leak!!!

  • Thread starter chintan jajal via .NET 247
  • Start date
C

chintan jajal via .NET 247

(Type your message here)
Hi all,

At this point of time i m totally confused... and really dont know what to do...
and it might be the case that my code is wrong... If yes then plz do correct me...

Here is my query :

We have a class which we want to call in async manner and this is just a test class.... but the problem is increasing memory.. so is there something which i m doing wrong

below is the code:

Imports System.Runtime.Remoting.Messaging

Class Writer
Delegate Function AsyncWriteText(ByVal strText As String) As String

Private Function WriteSomething(ByVal strToWrite As String) As String
System.Threading.Thread.Sleep(2000)
Return strToWrite
End Function

Public Function BeginWriteSomething(ByVal strToWrite As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Dim cd As AsyncWriteText = New AsyncWriteText(AddressOf WriteSomething)
Dim result As IAsyncResult = cd.BeginInvoke(strToWrite, callback, asyncState)
Return result
End Function

Public Function EndWriteSomething(ByVal Result As System.IAsyncResult) As String
Dim Test As AsyncWriteText = CType(CType(Result, AsyncResult).AsyncDelegate, AsyncWriteText)
Dim Obj As Object
Obj = Test.EndInvoke(Result)
Return CType(Obj, String)
End Function
End Class


OK above is the class which we want to call in asynchronously....

now this is our Form in which we call that method in a timer control and invoke it every 1 sec.


Public Class Form1
Inherits System.Windows.Forms.Form
Dim ObjWriter As New Writer


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.enabled = true
End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ObjWriter.BeginWriteSomething("chintan", AddressOf MyCallBack, Nothing)
End Sub

'and now is the callback
Private Sub MyCallBack(ByVal ar As IAsyncResult)
'Execute this function after the delegate runs
Dim str As String
str = ObjWriter.EndWriteSomething(ar)
'Just log the string nothing else.
End Sub


Now.... what happens its works fine..... but the memory keeps on increasing...and by some bytes.... eventually it will grow 1 mb after 15 minutes.

So i m confused.. where exactly the memory keeps on increasing.. Is that something which i need to dispose before calling the Async. method.

Plz. help and correct me if i m writing the delegates in a wrong manner...


Cheers
c
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top