Re: References problem
- From: "Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 08:37:41 -0700
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
>>
>
>
.
- Follow-Ups:
- Re: References problem
- From: Lloyd Dupont
- Re: References problem
- From: Michal Talaga
- Re: References problem
- References:
- References problem
- From: Michal Talaga
- Re: References problem
- From: Lloyd Dupont
- References problem
- Prev by Date: Re: IO Problem
- Next by Date: Re: System Null Exception always shows
- Previous by thread: Re: References problem
- Next by thread: Re: References problem
- Index(es):
Relevant Pages
|
Loading