tags:

views:

190

answers:

11

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()?

+7  A: 

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.

David M
you can still use using: `using (Pen p = GetAPen()) {...}`
tster
+6  A: 

Pen will not necessarily be garbage collected, but it will have it's Dispose() method called.

Kevin
+3  A: 

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.

Meta-Knight
+1  A: 

Yes it will be disposed.

tster
+1  A: 

It will be automatically disposed when the return is done. Using forces the use of Dispose() implicitely

AutomatedTester
+3  A: 

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;

}
Will
you shout put `System.Drawing.Pen pen` outside Try Catch block :)
serhio
I'm not exactly sure about that... The definition of the variable is within the using statement, so depending on the compiler implementation (which I'm not familiar with) it can lie either inside or outside. I can see how it can lay outside (preventing a NRE in the finally block).
Will
A: 

Your Pen object will be disposed before the return completes and you will be left with a pen object in an invalid state.

Jesse C. Slicer
+1  A: 

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.
}
Justin Niessner
A: 

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
    }
}
Joel Coehoorn
A: 

using is equals to the following statement roughly:

try {

     var something = new something() ;

}

finally {

 if (something != null)
 { something.Dispose();}   

}

Benny
+1  A: 

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...
}
BenAlabaster