Re: References problem



Hardly a memory leak. This is, as the OP suggested, a reference problem.
The GC can hardly be expected to recycle an object where all references
weren't disposed.

The override of the Dispose method that you suggest is not a workaround...
it is the expectation. It is normal and expected that a developer who
creates references to an object will be responsible for cleaning them up.

That said, I wonder if the problem isn't easier to handle by seperating the
connection to the UI from the business object itself.

In other words, the object that presents the event should be a seperate
object from the one that holds the data. When the form closes, the object
can be disposed of as well, thus dropping all references to the form. This
is a little more in keeping with the notion of Model-View-Controller in that
each object can have an object-level controller that ties the model to the
view. When the view is disposed, the controller can be tossed and a new one
created for a new view.

Therefore,
when a data item is created, add it to a singleton list where it can be
retrieved.

when a form is opened...
create a new controller for the data item
pass a reference to the data item into the controller
attach the event handler for the controller to the form.

when a form is closed, dispose of the controller as well.
The controller will drop and therefore, the reference to the form will
drop as well. However, the data will not be dropped because the singleton
list still points to it.

HTH,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Lloyd Dupont" <net.galador@ld> wrote in message
news:%23MYPvrEfFHA.2372@xxxxxxxxxxxxxxxxxxxxxxx
> congratulation!
> you just discover the "memory leak" still in .NET (even though it's
> garbage collected).
>
> fortunately there is a very simple fix. unfortunately manual and sometimes
> difficult to write.
>
> you should use disposable object (such as Form),
> override Dispose()
> where you unregister for all (external) event you are listeneing for
>
>
> When you think of it, that's a difficult issue, that garbage collection
> cannot fix, so you have to be carefull with event....
> and always unregister properly, when appropriate, by using a IDispose &
> Dispose() semantic
>
>
> "Michal Talaga" <mikeon@xxxxxxxxx> wrote in message
> news:%23fKW2bBfFHA.1504@xxxxxxxxxxxxxxxxxxxxxxx
>> Hello!
>>
>> In windows forms application I have an object MyObject with an event
>> SomeEvent.
>> I pass this object to a form to display its data. In this form I attach
>> an event hadler for SomeEvent.
>> Now, I close the first form and then the form goes out of scope, and pass
>> MyObject to some other form, which also attaches handler to SomeEvent.
>>
>> Now the problem is, even if the first form is closed/deleted (not
>> hidden), it will not be garbage collected, because of the reference from
>> MyObject (through the event handler).
>>
>> Is there a better way to solve this problem, other than manually removing
>> event handlers from MyObject.SomeEvent when closing the form?
>> It is of course a bigger problem than the situation described and having
>> to remember to remove hadlers from events is error prone.
>>
>> Any advice? I have a feeling that I'm missing something.
>>
>> --
>> Michal
>>
>
>


.



Relevant Pages

  • Re: events and object lifetime
    ... onto the objects with a direct reference and had a method to set the ... Whether you use Dispose() or some other method name, ... hanging around until your application terminates and a "true" memory ... garbage collector and concentrate on programming your application. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MVC Question
    ... Each view must be *given* a reference to the model and it should ... there must be a way to handle inputs and events - the controller. ... Classic MVC should be called MVCO, ... All graphics apps I've worked with have had a separate math ...
    (comp.object)
  • Re: Events unsubscribing and resource leaks?
    ... If you call an items dispose method, ... Setting a reference variable to null has no effect on ... If in your case you always know when your subscribing ... I then unsubscribe those instances that are subscribed to that, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C++/CLI is the way to go
    ... But the point is the smart pointer either doesn't need to know if it allocates the reference counter value externally, or it knows that the object is a reference counted one if you inherited from a IReferenceCounted interface or use attributes to add such code. ... But as I wrote in my other post, if you replace * with ^ and delete with Dispose it should also compile with managed ones. ... But I suppose the CLR teams would argue that there would be too much going on under the hood and it wouldn't be clear to the programmer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Memory Leak Experts!!!!
    ... pushed as a reference across the wire to some calling program on the ... doesn't influence the behavior of the collector. ... > you also implement the Finalize method which simply calles the Dispose ...
    (microsoft.public.dotnet.framework.performance)

Loading