Can someone explain what the zero point of an object is before it goes out of scope?

In several places, I saw code with the following logic:

public void func() { _myDictonary["foo"] = null; _myDictionary.Remove("foo"); } 

What is the point of setting foo to null in a dictionary before deleting it?

I thought garbage collection takes care of the number of things that indicate that it was originally foo. If this happens, is myDictonary ["foo"] equal to zero, just decrease the count by one? Will the same thing happen as soon as myDictonary.Remove ("foo") is called?

What is the meaning of _myDictonary ["foo"] = null;

edit: To clarify - when I said "delete account by one", I meant the following:
- myDictonary ["foo"] initially points to an object. This means that an object has one or more things that reference it.
- When myDictonary ["foo"] is set to null, it no longer refers to the specified object. This means that the object has fewer references to it.

+7
source share
8 answers

There is no point.

If you look at what the Remove method does (using .NET Reflector), you will find the following:

 this.entries[i].value = default(TValue); 

This line sets the value of the dictionary element to null, since the default value for the reference type is null. Thus, since the Remove method sets up a reference to null, there is no point in doing this before calling the method.

+13
source

Setting the dictionary entry to null does not reduce the number of links, since null is a perfectly suitable value indicated in the dictionary.

Both statements have different things. Setting it to null means that this value should be for this key, while removing this key from the dictionary means that it should no longer be.

+4
source

This does not make much sense.

However, if the Remove method causes heap distribution, and if the stored value is large, garbage collection can occur when you call Remove , and it can also collect the value in the process (potentially freeing memory). In practice, however, people usually do not worry about such trifles as was shown if it did not seem useful.


Edit:

I forgot to mention: ideally, the dictionary itself should worry about its own implementation, like this, and not about the caller.

+4
source

It doesn't make much sense, but there are times when it makes sense.

One example is in the Dispose() method. Consider this type:

 public class Owner { // snip private BigAllocation _bigAllocation; // snip protected virtual void Dispose(bool disposing) { if (disposing) { // free managed resources if (_bigAllocation != null) { _bigAllocation.Dispose(); _bigAllocation = null; } } } } 

Now you can argue that this is not necessary, and you will be basically right. Normally, Dispose() is called only before the Owner dereferenced, and when the Owner is assembled, _bigAllocation will also be ... after all.

But:

Setting _bigAllocation to null makes it suitable for collection right away if no one else references it. This can be beneficial if Owner is in a higher number GC generation or has a finalizer. Otherwise, Owner must be released before _bigAllocation is eligible to collect.

It is rather a corner case. Most types should not have finalizers, and in most cases _bigAllocation and Owner will be in the same generation.

+1
source

I think I could see that this is useful in a multi-threaded application where you zero out an object so that no other thread can work on it. Although, this seems like a heavy and poor design.

0
source

If this happens, myDictonary ["foo"] will not be set. Just reduce the score by one?

No, the counter does not change, the link is still in the dictionary, it points to zero.

I see no reason for the code to be as it is.

0
source

I don’t know about internal words in particular, but some types of collections may contain references to objects that are effectively β€œdead”. For example, a collection may contain an array and the number of valid elements in the array; zeroing the account will make any elements in the collection inaccessible, but will not destroy the links to them. Maybe deleting an element from the Dictionary ends up making an area that contains an object that is reusable, without actually deleting it. If another element with the same hash code is added to the dictionary, then the first element will actually be deleted, but this may not happen for a while, if ever.

0
source

This is similar to the old C ++ habit.

I suspect the author is worried about old collections and / or other languages. If memory is used, some collections in C ++ will contain pointers to assembled objects, and when β€œdelete” will only delete the pointer, but will not automatically call the destructor of the newly deleted object. This causes a very thin memory leak. The habit began to set the object to null before deleting it to make sure that the destructor was called.

0
source

All Articles