locked
Setting Object=Null sets it's child objects? RRS feed

  • Question

  • Hi
    I have defined a class like
    public class CSBusinessLogic
        {
            private CommonServiceDA commonServiceDA = new CommonServiceDA();
          
            public List<string> GetStates()
            {
                return commonServiceDA.GetStates();
            }    
         
            public StateCollection GetStatesOnLoad()
            {
                return commonServiceDA.GetStatesOnLoad();
               
            }
    }

    Now in my different  WCF methods i am creating an object of CSBusinessLogic and using it's methods. and after that setting it to null value.
    CSBusinessLogic ObjBusinessLogic  = new CSBusinessLogic();
    .
    .
    .
    ObjBusinessLogic = null;

    i would like to know once i set the parent object to null value, does it's child object (here commonServiceDA ) also to null value. or should i set it's child value also to null value, before setting parent object. I have thousands of wcf methods. If people start using it is there anyway to avoid memory leak? whats the best practice

           
    Friday, November 27, 2009 3:41 AM

Answers

  • They don't return to null but as you remove all of the references to the parent object it becomes eligible for garbage collection.  Once removed its child objects will also become unrefrfenanced and then will be cleaned up themselves.
    A programmer Trapped in a thugs body
    • Proposed as answer by rerun Friday, November 27, 2009 5:34 AM
    • Marked as answer by eryang Friday, December 4, 2009 3:22 AM
    Friday, November 27, 2009 5:34 AM
  • Hi,

    All unreachable objects are going to be GCed in a GC operation, and unreachable means an object is not reference directly or indirectly by the Root ( the Root includes global variables, static variables, etc).

    Here are some articles talking about GC related topic, hope they can help:

    http://www.codeproject.com/KB/aspnet/DONETBestPracticeNo2.aspx

    http://www.codeproject.com/KB/dotnet/BestPractices5.aspx

     

    Thanks,

    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Marked as answer by eryang Friday, December 4, 2009 3:22 AM
    Monday, November 30, 2009 7:52 AM

All replies

  • They don't return to null but as you remove all of the references to the parent object it becomes eligible for garbage collection.  Once removed its child objects will also become unrefrfenanced and then will be cleaned up themselves.
    A programmer Trapped in a thugs body
    • Proposed as answer by rerun Friday, November 27, 2009 5:34 AM
    • Marked as answer by eryang Friday, December 4, 2009 3:22 AM
    Friday, November 27, 2009 5:34 AM
  • Hi,

    All unreachable objects are going to be GCed in a GC operation, and unreachable means an object is not reference directly or indirectly by the Root ( the Root includes global variables, static variables, etc).

    Here are some articles talking about GC related topic, hope they can help:

    http://www.codeproject.com/KB/aspnet/DONETBestPracticeNo2.aspx

    http://www.codeproject.com/KB/dotnet/BestPractices5.aspx

     

    Thanks,

    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Marked as answer by eryang Friday, December 4, 2009 3:22 AM
    Monday, November 30, 2009 7:52 AM