Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Eventing and Memory leaks

Eventing and Memory leaks

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncareerperformancehelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RobCroll
    wrote on last edited by
    #1

    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."

    L L 2 Replies Last reply
    0
    • R RobCroll

      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."

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R RobCroll

        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."

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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:

        R 1 Reply Last reply
        0
        • L Lost User

          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:

          R Offline
          R Offline
          RobCroll
          wrote on last edited by
          #4

          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."

          N 1 Reply Last reply
          0
          • R RobCroll

            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."

            N Offline
            N Offline
            Nitin Singh India
            wrote on last edited by
            #5

            This is the reason if u register explicit events handlers at start , you should release them at end.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups