Eventing and Memory leaks
-
I went for a job interview a while ago and failed on this question. I have researched it but can not find a definitive answer. Can someone point me to definitive documentation which clearly explains disposing of objects which have subscribed or registered events. If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory? If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory? I was not aware of this issue but the person interviewing me seemed concerned. At the end of the day, I don't care, I'd just like to find out what the truth is regarding eventing and disposing of objects.
"You get that on the big jobs."
-
I went for a job interview a while ago and failed on this question. I have researched it but can not find a definitive answer. Can someone point me to definitive documentation which clearly explains disposing of objects which have subscribed or registered events. If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory? If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory? I was not aware of this issue but the person interviewing me seemed concerned. At the end of the day, I don't care, I'd just like to find out what the truth is regarding eventing and disposing of objects.
"You get that on the big jobs."
The question to ask yourself is: who is referencing whom? In both cases it is classB's event member that holds a reference to classA.
Robert Croll wrote:
If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
the subscription does not influence the life of classB, so it can be collected if it isn't alive anymore.
Robert Croll wrote:
If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
the subscription keeps classA alive for as long as classB is alive. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
I went for a job interview a while ago and failed on this question. I have researched it but can not find a definitive answer. Can someone point me to definitive documentation which clearly explains disposing of objects which have subscribed or registered events. If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory? If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory? I was not aware of this issue but the person interviewing me seemed concerned. At the end of the day, I don't care, I'd just like to find out what the truth is regarding eventing and disposing of objects.
"You get that on the big jobs."
Robert Croll wrote:
If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory? If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
That depends. Can they be reached by following references from "any" variable? If not, both will be removed from memory - if they're not static classes, that is :)
Robert Croll wrote:
I was not aware of this issue but the person interviewing me seemed concerned
..next time, ask for the answer and why he choose that particular question. It wouldn't make much sense to keep the answer a secret; you can find a huge amount of documentation with Google - it would merely prove that you interviewer knows how to differentiate between a correct answer (like Luc's) and a technical correct but completely useless answer (like mine). ..and you wanna know why he choose that question. Did they have a history of leaking things? It puts the interviewer in a defending position, giving you a few seconds to breathe and to collect your mind :)
I are Troll :suss:
-
Robert Croll wrote:
If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory? If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
That depends. Can they be reached by following references from "any" variable? If not, both will be removed from memory - if they're not static classes, that is :)
Robert Croll wrote:
I was not aware of this issue but the person interviewing me seemed concerned
..next time, ask for the answer and why he choose that particular question. It wouldn't make much sense to keep the answer a secret; you can find a huge amount of documentation with Google - it would merely prove that you interviewer knows how to differentiate between a correct answer (like Luc's) and a technical correct but completely useless answer (like mine). ..and you wanna know why he choose that question. Did they have a history of leaking things? It puts the interviewer in a defending position, giving you a few seconds to breathe and to collect your mind :)
I are Troll :suss:
Thanks Eddy, Both you and Luc have helped. The reason I asked was you also find a lot of misinformation on Google as well. But you have pointed me in the right direction. I think I'll be implementing weak events in future. BTW I like your approach to interview questions. I'll have to remember that :)
"You get that on the big jobs."
-
Thanks Eddy, Both you and Luc have helped. The reason I asked was you also find a lot of misinformation on Google as well. But you have pointed me in the right direction. I think I'll be implementing weak events in future. BTW I like your approach to interview questions. I'll have to remember that :)
"You get that on the big jobs."
This is the reason if u register explicit events handlers at start , you should release them at end.