Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will an implicit variable in a using clause be garbage collected?

In the following code, does the underlying code contain a hard reference to the unnamed variable instance of type Foo, or is the item vulnerable to garbage collection?

using(new Foo())
{
    // Something done here.
}

The collected item is just a semaphore type object that performs some reference counting on resources so it isn't being referenced in the code block.

like image 517
Steve Mitcham Avatar asked Dec 05 '25 16:12

Steve Mitcham


1 Answers

The using clause creates a hidden locally-scoped variable holding the object (this variable is used by the generated finally clause).

This variable prevents the object from being GC'd.

You can see this variable in the spec.

like image 72
SLaks Avatar answered Dec 08 '25 05:12

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!