Parent property leads the circular reference


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Parent property leads the circular reference

  1. #1
    Rattagarn V. Guest

    Parent property leads the circular reference


    Hi all,

    I create the active X control which contains a lot of classes. Some
    classes which are contained in this control have the parent property. This
    property is for setting the reference to the parent object of that class.
    Class MyClass:
    Private Sub Class_Terminate
    Set mParent = Nothing
    End Sub

    Friend Property Set Parent(vData as MyControl)
    If mParent Is Nothing Then
    Set mParent = vData
    End If
    End Property

    Usercontrol:
    Private Sub Usercontrol_Initialize()
    Set mMyClass = New MyClass
    Set mMyClass.Parent = Me
    End Sub

    Private Sub Usercontrol_Terminate()
    Set mMyClass = Nothing
    End Sub
    The problem is that the "Terminate" event of usercontrol never been executed
    after closing the Form which cantains this control.
    How can I clean up the object reference?

    Thank you very much
    Rattagarn V.

  2. #2
    Gagan Behl Guest

    Re: Parent property leads the circular reference


    Hi Rattagarn,

    I think you could use the forms' queryunload event to unload the object.


    Gagan

    "Rattagarn V." <rattagarn@hotmail> wrote:
    >
    >Hi all,
    >
    > I create the active X control which contains a lot of classes. Some
    >classes which are contained in this control have the parent property. This
    >property is for setting the reference to the parent object of that class.
    >Class MyClass:
    > Private Sub Class_Terminate
    > Set mParent = Nothing
    > End Sub
    >
    > Friend Property Set Parent(vData as MyControl)
    > If mParent Is Nothing Then
    > Set mParent = vData
    > End If
    > End Property
    >
    >Usercontrol:
    > Private Sub Usercontrol_Initialize()
    > Set mMyClass = New MyClass
    > Set mMyClass.Parent = Me
    > End Sub
    >
    > Private Sub Usercontrol_Terminate()
    > Set mMyClass = Nothing
    > End Sub
    >The problem is that the "Terminate" event of usercontrol never been executed
    >after closing the Form which cantains this control.
    >How can I clean up the object reference?
    >
    >Thank you very much
    >Rattagarn V.



  3. #3
    Rattagarn V. Guest

    Re: Parent property leads the circular reference


    Thank you for your response,

    I can't use the forms' queryunload event because my usercontrol will
    be placed on the another usercontrol.
    I develop the web application which contains my control. I used a usercontrol
    as the web page area and also have the array of usercontrol as frames for
    holding each activeX control which I created. So user can switch to or add
    more frames. The problem is that after any frame the terminated event of
    each activeX control in that frame has never raised the terminated event.
    I read the MSDN on-line and it advises to use TearDown method. It means
    that I have to implement a method that is called for setting nothing to each
    instances in my activeX control. Yes! it works, but why I don't need to
    call any function of a button control likes this when the form or usercontrol
    is terminated. It should be the same concept. Please advise me.
    Rattagarn V.
    "Gagan Behl" <gbehl@indya.com> wrote:
    >
    >Hi Rattagarn,
    >
    >I think you could use the forms' queryunload event to unload the object.
    >
    >
    >Gagan
    >
    >"Rattagarn V." <rattagarn@hotmail> wrote:
    >>
    >>Hi all,
    >>
    >> I create the active X control which contains a lot of classes. Some
    >>classes which are contained in this control have the parent property.

    This
    >>property is for setting the reference to the parent object of that class.
    >>Class MyClass:
    >> Private Sub Class_Terminate
    >> Set mParent = Nothing
    >> End Sub
    >>
    >> Friend Property Set Parent(vData as MyControl)
    >> If mParent Is Nothing Then
    >> Set mParent = vData
    >> End If
    >> End Property
    >>
    >>Usercontrol:
    >> Private Sub Usercontrol_Initialize()
    >> Set mMyClass = New MyClass
    >> Set mMyClass.Parent = Me
    >> End Sub
    >>
    >> Private Sub Usercontrol_Terminate()
    >> Set mMyClass = Nothing
    >> End Sub
    >>The problem is that the "Terminate" event of usercontrol never been executed
    >>after closing the Form which cantains this control.
    >>How can I clean up the object reference?
    >>
    >>Thank you very much
    >>Rattagarn V.

    >



  4. #4
    Gagan Behl Guest

    Re: Parent property leads the circular reference


    Rattagarn :

    I fail to understand one thing clearly. When an ActiveX control's life is
    limited to that of a page, then why are you worrying about destroying the
    instances in the first place ?

    Gagan


    "Rattagarn V." <rattagarn@hotmail.com> wrote:
    >
    >Thank you for your response,
    >
    > I can't use the forms' queryunload event because my usercontrol will
    >be placed on the another usercontrol.
    > I develop the web application which contains my control. I used a usercontrol
    >as the web page area and also have the array of usercontrol as frames for
    >holding each activeX control which I created. So user can switch to or

    add
    >more frames. The problem is that after any frame the terminated event of
    >each activeX control in that frame has never raised the terminated event.
    > I read the MSDN on-line and it advises to use TearDown method. It means
    >that I have to implement a method that is called for setting nothing to

    each
    >instances in my activeX control. Yes! it works, but why I don't need to
    >call any function of a button control likes this when the form or usercontrol
    >is terminated. It should be the same concept. Please advise me.
    >Rattagarn V.
    >"Gagan Behl" <gbehl@indya.com> wrote:
    >>
    >>Hi Rattagarn,
    >>
    >>I think you could use the forms' queryunload event to unload the object.
    >>
    >>
    >>Gagan
    >>
    >>"Rattagarn V." <rattagarn@hotmail> wrote:
    >>>
    >>>Hi all,
    >>>
    >>> I create the active X control which contains a lot of classes. Some
    >>>classes which are contained in this control have the parent property.


    >This
    >>>property is for setting the reference to the parent object of that class.
    >>>Class MyClass:
    >>> Private Sub Class_Terminate
    >>> Set mParent = Nothing
    >>> End Sub
    >>>
    >>> Friend Property Set Parent(vData as MyControl)
    >>> If mParent Is Nothing Then
    >>> Set mParent = vData
    >>> End If
    >>> End Property
    >>>
    >>>Usercontrol:
    >>> Private Sub Usercontrol_Initialize()
    >>> Set mMyClass = New MyClass
    >>> Set mMyClass.Parent = Me
    >>> End Sub
    >>>
    >>> Private Sub Usercontrol_Terminate()
    >>> Set mMyClass = Nothing
    >>> End Sub
    >>>The problem is that the "Terminate" event of usercontrol never been executed
    >>>after closing the Form which cantains this control.
    >>>How can I clean up the object reference?
    >>>
    >>>Thank you very much
    >>>Rattagarn V.

    >>

    >



  5. #5
    Rattagarn V. Guest

    Re: Parent property leads the circular reference


    Thank you for you response Gagan:
    Even my web application has one page only, user can add (load/unload)
    any activex control they want. The problem is that each time they add a
    control, it will consume the resources and memory of the system. Unfortunately,
    the resources and memory are never backed after unload these controls. So
    while user uses the application, the resources is reduced continueously.
    The resources has never reclaimed because the terminated event of each control
    has never been reached because of circular reference. How can I solve this?

    Rattagarn V.

    "Gagan Behl" <gbehl@indya.com> wrote:
    >
    >Rattagarn :
    >
    >I fail to understand one thing clearly. When an ActiveX control's life is
    >limited to that of a page, then why are you worrying about destroying the
    >instances in the first place ?
    >
    >Gagan
    >
    >
    >"Rattagarn V." <rattagarn@hotmail.com> wrote:
    >>
    >>Thank you for your response,
    >>
    >> I can't use the forms' queryunload event because my usercontrol will
    >>be placed on the another usercontrol.
    >> I develop the web application which contains my control. I used a usercontrol
    >>as the web page area and also have the array of usercontrol as frames for
    >>holding each activeX control which I created. So user can switch to or

    >add
    >>more frames. The problem is that after any frame the terminated event

    of
    >>each activeX control in that frame has never raised the terminated event.
    >> I read the MSDN on-line and it advises to use TearDown method. It means
    >>that I have to implement a method that is called for setting nothing to

    >each
    >>instances in my activeX control. Yes! it works, but why I don't need to
    >>call any function of a button control likes this when the form or usercontrol
    >>is terminated. It should be the same concept. Please advise me.
    >>Rattagarn V.
    >>"Gagan Behl" <gbehl@indya.com> wrote:
    >>>
    >>>Hi Rattagarn,
    >>>
    >>>I think you could use the forms' queryunload event to unload the object.
    >>>
    >>>
    >>>Gagan
    >>>
    >>>"Rattagarn V." <rattagarn@hotmail> wrote:
    >>>>
    >>>>Hi all,
    >>>>
    >>>> I create the active X control which contains a lot of classes.

    Some
    >>>>classes which are contained in this control have the parent property.

    >
    >>This
    >>>>property is for setting the reference to the parent object of that class.
    >>>>Class MyClass:
    >>>> Private Sub Class_Terminate
    >>>> Set mParent = Nothing
    >>>> End Sub
    >>>>
    >>>> Friend Property Set Parent(vData as MyControl)
    >>>> If mParent Is Nothing Then
    >>>> Set mParent = vData
    >>>> End If
    >>>> End Property
    >>>>
    >>>>Usercontrol:
    >>>> Private Sub Usercontrol_Initialize()
    >>>> Set mMyClass = New MyClass
    >>>> Set mMyClass.Parent = Me
    >>>> End Sub
    >>>>
    >>>> Private Sub Usercontrol_Terminate()
    >>>> Set mMyClass = Nothing
    >>>> End Sub
    >>>>The problem is that the "Terminate" event of usercontrol never been executed
    >>>>after closing the Form which cantains this control.
    >>>>How can I clean up the object reference?
    >>>>
    >>>>Thank you very much
    >>>>Rattagarn V.
    >>>

    >>

    >



  6. #6
    Michael Culley Guest

    Re: Parent property leads the circular reference


    Rattagarn,

    >because of circular reference


    This is your answer here. You have to avoid circular references. To do this
    you have to keep a reference to your usercontrol without your usercontrol
    knowing you have it. In the class, change the parent Set/Get to this

    private mlngParent as long

    Public property Set Parent(ByVal RHS as MyControl)
    mlnParent=objptr(rhs)
    end property

    Public property Get Parent() as MyControl
    Set MyControl=ResolveObjPtr ( mlngParent)
    End Property

    Private Function ResolveObjPtr(ByVal Pointer as long) as object
    Dim x as object
    copymemory x,pointer,4&
    set resolveobjptr=x
    copymemory x,0&,4&
    end function

    Then in your usercontrol Terminate event

    Set MyClass.Parent=Nothing

    There is plenty of info on this topic all over the web. Just search for stolen
    reference, cicular reference or weak reference.


    Michael Culley


    "Rattagarn V." <rattagarn@hotmail.com> wrote:
    >
    >Thank you for you response Gagan:
    > Even my web application has one page only, user can add (load/unload)
    >any activex control they want. The problem is that each time they add a
    >control, it will consume the resources and memory of the system. Unfortunately,
    >the resources and memory are never backed after unload these controls.

    So
    >while user uses the application, the resources is reduced continueously.
    > The resources has never reclaimed because the terminated event of each

    control
    >has never been reached because of circular reference. How can I solve this?
    >
    >Rattagarn V.
    >
    >"Gagan Behl" <gbehl@indya.com> wrote:
    >>
    >>Rattagarn :
    >>
    >>I fail to understand one thing clearly. When an ActiveX control's life

    is
    >>limited to that of a page, then why are you worrying about destroying the
    >>instances in the first place ?
    >>
    >>Gagan
    >>
    >>
    >>"Rattagarn V." <rattagarn@hotmail.com> wrote:
    >>>
    >>>Thank you for your response,
    >>>
    >>> I can't use the forms' queryunload event because my usercontrol will
    >>>be placed on the another usercontrol.
    >>> I develop the web application which contains my control. I used a

    usercontrol
    >>>as the web page area and also have the array of usercontrol as frames

    for
    >>>holding each activeX control which I created. So user can switch to or

    >>add
    >>>more frames. The problem is that after any frame the terminated event

    >of
    >>>each activeX control in that frame has never raised the terminated event.
    >>> I read the MSDN on-line and it advises to use TearDown method. It

    means
    >>>that I have to implement a method that is called for setting nothing to

    >>each
    >>>instances in my activeX control. Yes! it works, but why I don't need

    to
    >>>call any function of a button control likes this when the form or usercontrol
    >>>is terminated. It should be the same concept. Please advise me.
    >>>Rattagarn V.
    >>>"Gagan Behl" <gbehl@indya.com> wrote:
    >>>>
    >>>>Hi Rattagarn,
    >>>>
    >>>>I think you could use the forms' queryunload event to unload the object.
    >>>>
    >>>>
    >>>>Gagan
    >>>>
    >>>>"Rattagarn V." <rattagarn@hotmail> wrote:
    >>>>>
    >>>>>Hi all,
    >>>>>
    >>>>> I create the active X control which contains a lot of classes.


    >Some
    >>>>>classes which are contained in this control have the parent property.

    >>
    >>>This
    >>>>>property is for setting the reference to the parent object of that class.
    >>>>>Class MyClass:
    >>>>> Private Sub Class_Terminate
    >>>>> Set mParent = Nothing
    >>>>> End Sub
    >>>>>
    >>>>> Friend Property Set Parent(vData as MyControl)
    >>>>> If mParent Is Nothing Then
    >>>>> Set mParent = vData
    >>>>> End If
    >>>>> End Property
    >>>>>
    >>>>>Usercontrol:
    >>>>> Private Sub Usercontrol_Initialize()
    >>>>> Set mMyClass = New MyClass
    >>>>> Set mMyClass.Parent = Me
    >>>>> End Sub
    >>>>>
    >>>>> Private Sub Usercontrol_Terminate()
    >>>>> Set mMyClass = Nothing
    >>>>> End Sub
    >>>>>The problem is that the "Terminate" event of usercontrol never been

    executed
    >>>>>after closing the Form which cantains this control.
    >>>>>How can I clean up the object reference?
    >>>>>
    >>>>>Thank you very much
    >>>>>Rattagarn V.
    >>>>
    >>>

    >>

    >



  7. #7
    Bill Storage Guest

    Re: Parent property leads the circular reference

    If you'd like more details on Michaels solution, I've got an article on my
    site that might help:

    http://www.bstorage.com/NerveNet/Sub...hAddressOf.asp

    Bill Storage



    Rattagarn V. <rattagarn@hotmail.com> wrote in message
    news:39c21284$1@news.devx.com...
    >
    > Thank you for you response Gagan:
    > Even my web application has one page only, user can add (load/unload)
    > any activex control they want. The problem is that each time they add a
    > control, it will consume the resources and memory of the system.

    Unfortunately,
    > the resources and memory are never backed after unload these controls. So
    > while user uses the application, the resources is reduced continueously.
    > The resources has never reclaimed because the terminated event of each

    control
    > has never been reached because of circular reference. How can I solve

    this?
    >
    > Rattagarn V.
    >
    > "Gagan Behl" <gbehl@indya.com> wrote:
    > >
    > >Rattagarn :
    > >
    > >I fail to understand one thing clearly. When an ActiveX control's life is
    > >limited to that of a page, then why are you worrying about destroying the
    > >instances in the first place ?
    > >
    > >Gagan
    > >
    > >
    > >"Rattagarn V." <rattagarn@hotmail.com> wrote:
    > >>
    > >>Thank you for your response,
    > >>
    > >> I can't use the forms' queryunload event because my usercontrol will
    > >>be placed on the another usercontrol.
    > >> I develop the web application which contains my control. I used a

    usercontrol
    > >>as the web page area and also have the array of usercontrol as frames

    for
    > >>holding each activeX control which I created. So user can switch to or

    > >add
    > >>more frames. The problem is that after any frame the terminated event

    > of
    > >>each activeX control in that frame has never raised the terminated

    event.
    > >> I read the MSDN on-line and it advises to use TearDown method. It

    means
    > >>that I have to implement a method that is called for setting nothing to

    > >each
    > >>instances in my activeX control. Yes! it works, but why I don't need to
    > >>call any function of a button control likes this when the form or

    usercontrol
    > >>is terminated. It should be the same concept. Please advise me.
    > >>Rattagarn V.
    > >>"Gagan Behl" <gbehl@indya.com> wrote:
    > >>>
    > >>>Hi Rattagarn,
    > >>>
    > >>>I think you could use the forms' queryunload event to unload the

    object.
    > >>>
    > >>>
    > >>>Gagan
    > >>>
    > >>>"Rattagarn V." <rattagarn@hotmail> wrote:
    > >>>>
    > >>>>Hi all,
    > >>>>
    > >>>> I create the active X control which contains a lot of classes.

    > Some
    > >>>>classes which are contained in this control have the parent property.

    > >
    > >>This
    > >>>>property is for setting the reference to the parent object of that

    class.
    > >>>>Class MyClass:
    > >>>> Private Sub Class_Terminate
    > >>>> Set mParent = Nothing
    > >>>> End Sub
    > >>>>
    > >>>> Friend Property Set Parent(vData as MyControl)
    > >>>> If mParent Is Nothing Then
    > >>>> Set mParent = vData
    > >>>> End If
    > >>>> End Property
    > >>>>
    > >>>>Usercontrol:
    > >>>> Private Sub Usercontrol_Initialize()
    > >>>> Set mMyClass = New MyClass
    > >>>> Set mMyClass.Parent = Me
    > >>>> End Sub
    > >>>>
    > >>>> Private Sub Usercontrol_Terminate()
    > >>>> Set mMyClass = Nothing
    > >>>> End Sub
    > >>>>The problem is that the "Terminate" event of usercontrol never been

    executed
    > >>>>after closing the Form which cantains this control.
    > >>>>How can I clean up the object reference?
    > >>>>
    > >>>>Thank you very much
    > >>>>Rattagarn V.
    > >>>
    > >>

    > >

    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center