开发者

Why does this singleton behaviour occur?

One of my junior programmers has created a singleton, but it is acting strangely:

I know you aren't supposed to access object like this, but that's how they did it and I can't explain why this is occurring - After calling remove instance, I put a breakpoint on the line after and I can still access the someObject object and it's properties. This kind of makes sense as your access the reference of the of the mySingleton Object, not the myInstance,... as as you can see I can't explain it clearly, can anyone help?

e.g.

    Dim x As MySingleton = MySingleton.GetInstance()

    x.someObject.int = 5
    x.someObject.str = "hello"

    Console.Out.WriteLine(x.someObject.int.ToString)
    Console.Out.WriteLine(x.someObject.str.ToString)

    MySingleton.RemoveInstance()

    Console.Out.WriteLine(x.someObject.int.ToString) //still exists!
    开发者_运维问答Console.Out.WriteLine(x.someObject.str.ToString) //still exists!

Here's the Psuedo Code for the singleton:

Public Class MySingleton

    Private Shared _myInstance As MySingleton

    Public someObject As New Class1

    Public Shared Function GetInstance() As MySingleton
        If _myInstance Is Nothing Then
            _myInstance = New MySingleton
        End If
        Return _myInstance

    End Function

    Public Shared Sub RemoveInstance()
        _myInstance = Nothing
    End Sub


    End Class

Personally I don't write my singletons like this - I have the instance object as a separate class. but each to their own.


It is because you only removed the MySingleton._myInstance reference to the object.

x still has a reference to that object.

Edit:
To clarify:

it is like:

var A = new Class();
var B = A;
A = null;
// Here B still has that reference to the created object


The first line gets a reference to your singleton, and that reference will remain valid until the object is garbage collected. However, as long as there are still references to an object it will not be garbage collected - so you will continue to have a reference to a valid object until you explicitly assign x to something else.


In general, in .NET you can never explicitly/deterministically set an object to nothing. You can only set its references to Nothing and then allow the garbage collector to clean it up. As already stated, in your original code you actually retained a reference to it by assigning it to x.

In general, setting an object to Nothing or null is not suggested because an object is eligible for garbage collection after the last reference; the runtime notices this. For example, if you have code like this:

Dim anObject as new MyObject

anObject.DoAnOperation()
[1]
... 
...
'more code
...
...

Set anObject = Nothing
[2]

If you did not set the object to Nothing, it would be eligible for collection anytime after point [1]. If you do, it is not eligible until point [2]. If your method takes a long time to complete, it could become important.

More info:

http://msdn.microsoft.com/en-us/library/ee787088.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜