Delegates Memory Leak!!!

Discussion in 'ASP .Net' started by chintan jajal via .NET 247, May 26, 2005.

  1. (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
    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...

    From: chintan jajal

    Posted by a user from .NET 247 (

    chintan jajal via .NET 247, May 26, 2005
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gopal Prabhakaran

    Differnece between Events and Delegates

    Gopal Prabhakaran, Jan 26, 2004, in forum: ASP .Net
    Gaurav Khanna [C# MVP]
    Jan 28, 2004
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Richard Heathfield
    Jul 10, 2006
  4. cham
  5. Mark Probert
    Mark Probert
    Feb 9, 2005

Share This Page