Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
400
ConditionValueAppearance is not removing itself from Appearance.SubObjectPropChanged event list
posted

Hello,

I recently discovered that the delegate reference placed on the Appearance object in the ConditionValueAppearance constructor was preventing my usercontrol from being garbage collected.  Whenever an object places a delegate to itself on another objects event list, there has to be some mechanism to remove the delegate from the event otherwise the reference will prevent the object from being garbage collected.  This is the case for ConditionValueAppearance objects.  It's lifetime is determined by the lifetime of the Appearance object unless a third party removes the Conditions from the ConditionValueAppearance object.

What is the recommended way to make sure these ConditionValueAppearance objects are not leaked?   Currently it looks like the user has to make sure Clear() is called at some point when the grid is disposed. 

I realize its up to the user to control the life span of the ConditionValueAppearance that he/she created as it could potentially be shared across grids etc.  However I don't ever remember reading about this anywhere

  • 69832
    Offline posted

    I looked into this and from what I saw with a simple test, as long as you dispose of the grid the ConditionValueAppearance will be dereferenced, thus allowing it to be garbage collected. The grid disposes of its columns, and the column dereferences its pointer to the ValueBasedAppearance assigned to it, so the grid will not cause anything to be rooted there. The fact that the Appearance's SubObjectPropChanged event is never unhooked does not matter because nothing has a reference to the ConditionValueAppearance, so the garbage collector will get rid of it.

    Having said that, you can hook the grid's Disposed event so that you receive a notification when the control is disposed of, and manually call the Clear method on each ConditionValueAppearance your created, which will detach the SubObjectPropChanged delegate immediately, if you are concerned about waiting for the garbage collector to get around to that generation. Admittedly,  this is kind of a nuisance, but the grid is not really supposed to make assumptions about an object that you created, i.e., it really isn't responsible for disposing of it.