CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2006
    Posts
    220

    effect of assigning a NULL

    Hi

    Lets say we have an object

    Code:
    MyClass o = new MyClass();
    and we write following line

    Code:
    o = null;
    What will be the effect of assigning a null to some object. Will the actual object allocated on heap be cleaned or just the reference to that object on heap be cleaned and the actual object on heap will be cleaned when Garbage Collector runs?

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb Re: effect of assigning a NULL

    Take a look at this links:

    http://msdn.microsoft.com/en-us/libr...38(VS.71).aspx
    http://www.codeproject.com/KB/cs/csavoidnull.aspx

    o become eligible for garbage collection when the variable o is assigned the value null.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: effect of assigning a NULL

    There are two effects:
    1. If the "o" is the only reference to the instance of the MyClass, then the instance becames subject to GC.
    2. If you call any method on the "o", then you the NullReferenceException will occure.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: effect of assigning a NULL

    You can learn about the .NET CLR garbage collector here.

    If you want more details about the inner workings of .NET then buy "CLR via C#" by Richter - it's kind of the bible when it comes to .NET.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: effect of assigning a NULL

    The ONLYtime you should explicitly assign null (lower call..upper case NULL is a C++ item!) is when the variable holding the reference is going to remain in scope past the point of last use.

    In 90%+ of the cases the variable stying in scope longer than the lifetime of the reference is an indication of a bug in the architecture.

    Consider:
    Code:
    int f(int x)
    {
        MyClass c = new MyClass(); /// object created.
         // other code
         c.SomeMethod(...); 
         // more code that does NOT use "c", and does not loop back to before the above line
    }
    c is actually available for garbage collection immediately after the call to c.SomeMethod(...) iun an optimized build.

    To make it elegible in a non-optimized build:

    Code:
    int f(int x)
    {
        {
            MyClass c = new MyClass(); /// object created.
             // other code
            c.SomeMethod(...); 
        }
         // more code that does NOT use "c", and does not loop back to before the above line
    }
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Apr 2006
    Posts
    220

    Re: effect of assigning a NULL

    Quote Originally Posted by TheCPUWizard View Post
    To make it elegible in a non-optimized build:

    Code:
    int f(int x)
    {
        {
            MyClass c = new MyClass(); /// object created.
             // other code
            c.SomeMethod(...); 
        }
         // more code that does NOT use "c", and does not loop back to before the above line
    }
    What do you mean by "To make it elegible in a non-optimized build:" Do you mean that by putting our code in some braces ensures that NULL references be cleaned early.

  7. #7
    Join Date
    Apr 2006
    Posts
    220

    Re: effect of assigning a NULL

    My actual question was about the deallocation of memory assigned to some objects in heap. Does GC deallocate the memory allocated to some object immediately or does it wait for next GC cycle, when we assign NULL to some object.

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: effect of assigning a NULL

    No, the memory is not deallocated immediately, but if the GC occures (which can be whenever, based on memory allocation needs). The memory is deallocated if there is no reference, setting to null must not be enought, because the object can be referenced e.g. somewhere in stack or as member variable in other object.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: effect of assigning a NULL

    1) This depends on your meaning of "Deallocated"....If you mean making the memory available for future calls to new, then boudino is correct...If you mean making it available to the system, that is a completely different. In general once the process aquires memory from the system for Gen0 and Gen1, it is NEVER returned to the OS [until the process exits].


    2) For an object to be "released", there must be NO active references (except possible weak references) to the value, and the GC must run for the generation which owns the object. Re-read this until you you start saying this in your sleep.

    In an optimized build, then compiler is smart enough to realize where the last possible usage of a variable within a scope is. So immediately after this, even though the reference is in scope, it is not active (see above paragraph).

    In a non-optimized build, the compiler treats the reference as active until the actual scope terminates. This makes debugging easier.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured