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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. General object destruction question:

General object destruction question:

Scheduled Pinned Locked Moved C#
questionperformancehelplounge
2 Posts 2 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.
  • T Offline
    T Offline
    tantiboh
    wrote on last edited by
    #1

    Being an intermediate programmer, I'm having a hard time understanding the documentation that is out there regarding this topic. Let's say I have two objects, derived from ClassA and ClassB. The lifecycle of Class B is as follows: -ClassA instantiates an instance of ClassB, and places this object into, say, a hashtable. -ClassB does its thing for a while, but then becomes useless. -ClassA removes ClassB from the hashtable. Now, there is no reference to that object within ClassA. What happens to ClassB? Is it still floating out there in memory? Do I need to tell ClassA to actually destroy the ClassB object, or does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Thanks for your help.

    M 1 Reply Last reply
    0
    • T tantiboh

      Being an intermediate programmer, I'm having a hard time understanding the documentation that is out there regarding this topic. Let's say I have two objects, derived from ClassA and ClassB. The lifecycle of Class B is as follows: -ClassA instantiates an instance of ClassB, and places this object into, say, a hashtable. -ClassB does its thing for a while, but then becomes useless. -ClassA removes ClassB from the hashtable. Now, there is no reference to that object within ClassA. What happens to ClassB? Is it still floating out there in memory? Do I need to tell ClassA to actually destroy the ClassB object, or does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Thanks for your help.

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      tantiboh wrote: Is it still floating out there in memory? Yes, for an indeterminate amount of time, until the GC puts the memory back into the general pool ("collects" it). tantiboh wrote: Do I need to tell ClassA to actually destroy the ClassB object No. Not unless ClassB was using some non-managed resources. does the GC already know that ClassA no longer refers to ClassB, and therefore destroys it automatically? Yes. It's hard to get ones arms around this, coming from C++. There's a lot of good articles out there. Try googling on "Understanding .NET Garbage Collection". Marc MyXaml Advanced Unit Testing YAPO

      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