Reputation: 28586
In the example below, will the returned pen
be destroyed(disposed) or not?
' VB'
Public Function GetPen() As System.Drawing.Pen
Using pen As New System.Drawing.Pen(_Color, _Width)
pen.DashStyle = _DashStyle
Return pen
End Using
End Function
// C#
public System.Drawing.Pen GetPen()
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width))
{
pen.DashStyle = _DashStyle;
return pen;
}
}
[EDIT]
Just one more precision... Is the Pen object sent to the caller of GetPen by reference or 'cloned' like a structure? I know, this is a class, but with GDI objects I am never sure...
Will it be destroyed(disposed) the pen
created in GetPen()
when the external method will Dispose its pen
obtained with GetPen()
?
Upvotes: 5
Views: 335
Reputation: 17845
Yes, pen will be disposed. It's really a bad idea though; you return a pen that's already disposed!
What you want to do is to remove the Using statement from GetPen. The Using statement should be used by the GetPen callers:
Using pen As Pen = GetPen()
''// Draw with this pen
End Using
Or in C#:
using(Pen pen = GetPen())
{
// Draw with this pen
}
[EDIT]
Yes, a reference is returned to the calling method, not a copy. That's why if you dispose of the pen in GetPen, you can't use that pen in the calling method ;-)
Because of the fact that GetPen and the calling method point to the same Pen object, you just need to call Dispose in the calling method.
Upvotes: 3
Reputation: 39836
Your code needs to be structured differently because you're returning an object that's then immediately disposed of.
public System.Drawing.Pen GetPen()
{
System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width)
{
DashStyle = _DashStyle;
}
return pen;
}
Then call it using:
using (System.Drawing.Pen pen = GetPen())
{
//Do stuff with your pen...
}
Upvotes: 1
Reputation: 8815
using is equals to the following statement roughly:
try {
var something = new something() ;
}
finally {
if (something != null)
{ something.Dispose();}
}
Upvotes: 0
Reputation: 415800
Yes. In both the C# and VB.net versions of your code, the .Dispose() method is called before the function returns. That is not the same thing as destroying it (the object still exists) but for all practical purposes the object is useless, because the unmanaged resource that made it interesting and useful is now released.
The correct pattern to use when building an IDisposable resource inside a function is to just return it, and use a using declaration to call the function, like this:
public System.Drawing.Pen GetPen()
{
var pen = new System.Drawing.Pen(_Color, _Width))
pen.DashStyle = _DashStyle;
return pen;
}
public void FunctionThatCallsGetPen()
{
using (System.Drawing.Pen pen = GetPen())
{
//use pen here
}
}
Upvotes: 0
Reputation: 245429
Yes.
The returned Pen object will be Disposed before the caller receives it.
In Get()
style methods, you don't want to deal with Disposing of objects. Allow the caller to Dispose of the object AFTER they are done with it. I would expect the calling function to look like this:
using(Pen myPen = GetPen())
{
// My code here.
}
Upvotes: 1
Reputation: 20157
Your Pen
object will be disposed before the return completes and you will be left with a pen object in an invalid state.
Upvotes: 0
Reputation:
The pen will be disposed before returning. Its the equivalent of
public System.Drawing.Pen GetPen()
{
try
{
System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width);
pen.DashStyle = _DashStyle;
}
finally
{
pen.Dispose();
}
return pen;
}
Upvotes: 3
Reputation: 22418
It will be automatically disposed when the return is done. Using forces the use of Dispose() implicitely
Upvotes: 1
Reputation: 115488
Pen will not necessarily be garbage collected, but it will have it's Dispose() method called.
Upvotes: 6
Reputation: 72870
Yes, it will be disposed. You are then returning it in a disposed state, so it won't be any good for anything. If you want to use a factory method like this to return a Pen
instance, you'll need to deal with disposing it yourself externally to the method, not using a using
block within the method like this.
Upvotes: 7