Click here to Skip to main content
12,350,451 members (23,534 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.6K views
1 download
5 bookmarked
Posted

Life after Death: Nullified Reference Looks Still Active

, 21 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
After reference to instance of a class was set to null, it seemed it was still active

One morning, still drinking my usual strong black coffee I was debugging some code that seemed fairly routine. But behavior of a code fragment looking simple appeared somewhat abnormal. Soon I realized that I was dealing with something like this (after removing all irrelevant code):

var c = new C();

//...........................

c = null;

//...........................

c.Foo();  // no exception, execution goes on!

After reference to instance of a class was set to null, it seemed it was still active! And I was pretty sure that there were no tricks with regeneration on finalization.

Well, may be coffee was not strong enough. Or perhaps something wrong with my new glasses... I had repeated debugging - with the same result.

It took me some time to realize what's going on. Extension methods were added to C# for quite a time already, but not very wide spread. This is simplified code allowing the above behavior:

class C
{
    //...................
}

static class CExtension
{
    public static void Foo(this C c) 
    {
        if (c == null)
            return;

        //...................
    }
}

Such a simple explanation! I found it funny and happy to share it with you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Igor Ladnik
Architect
Israel Israel


  • Nov 2010: Code Project Contests - Windows Azure Apps - Winner
  • Feb 2011: Code Project Contests - Windows Azure Apps - Grand Prize Winner


You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Sebastien GASPAR6-May-14 6:16
memberSebastien GASPAR6-May-14 6:16 
GeneralMy vote of 5 Pin
Rajesh Varma Buddaraju22-Apr-14 19:52
memberRajesh Varma Buddaraju22-Apr-14 19:52 
GeneralRe: My vote of 5 Pin
Igor Ladnik22-Apr-14 20:16
professionalIgor Ladnik22-Apr-14 20:16 
GeneralNothing new Pin
PIEBALDconsult22-Apr-14 9:57
memberPIEBALDconsult22-Apr-14 9:57 
GeneralRe: Nothing new Pin
Igor Ladnik22-Apr-14 20:18
professionalIgor Ladnik22-Apr-14 20:18 
GeneralMy vote of 5 Pin
Bruno Tagliapietra22-Apr-14 4:37
memberBruno Tagliapietra22-Apr-14 4:37 
GeneralRe: My vote of 5 Pin
Igor Ladnik22-Apr-14 7:30
professionalIgor Ladnik22-Apr-14 7:30 
GeneralI don't think its funny.. Pin
Suvabrata Roy21-Apr-14 19:10
professionalSuvabrata Roy21-Apr-14 19:10 
QuestionMy vote of 5 Pin
Emre Ataseven21-Apr-14 10:21
memberEmre Ataseven21-Apr-14 10:21 
AnswerRe: My vote of 5 Pin
Igor Ladnik21-Apr-14 16:31
professionalIgor Ladnik21-Apr-14 16:31 
GeneralThank you Pin
Tesfamichael G.21-Apr-14 6:04
memberTesfamichael G.21-Apr-14 6:04 
GeneralRe: Thank you Pin
Igor Ladnik21-Apr-14 6:14
professionalIgor Ladnik21-Apr-14 6:14 
GeneralMy vote of 4 Pin
jlopez78821-Apr-14 5:42
memberjlopez78821-Apr-14 5:42 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 5:53
professionalIgor Ladnik21-Apr-14 5:53 
GeneralRe: My vote of 4 Pin
jlopez78821-Apr-14 13:10
memberjlopez78821-Apr-14 13:10 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 16:35
professionalIgor Ladnik21-Apr-14 16:35 
GeneralRe: My vote of 4 Pin
Oleg A.Lukin21-Apr-14 19:20
memberOleg A.Lukin21-Apr-14 19:20 
GeneralRe: My vote of 4 Pin
Igor Ladnik21-Apr-14 22:32
professionalIgor Ladnik21-Apr-14 22:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 21 Apr 2014
Article Copyright 2014 by Igor Ladnik
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid