0

I am wondering if in the given scenario below, any manual Bitmap dispose action need take place. Let's say I have two Bitmaps global to my WinForm, A and B. Lets say I want to do some transform/blend operation on their respective images, but want to do so on copies. I aim to call the transform function (Bitmap Transform(Bitmap first, Bitmap second)) similarly to this:

var newB = Transform( ( Bitmap )A.Clone(), ( Bitmap )B.Clone() ).

When done in Transform, do I dispose of the A and B copies within transform? Do I copy before the invocation and then dispose after the return? Since these references are absolutely lost after the Transform return, can I trust the GC?

-EDIT_ ADDING COMMENT FROM BELOW_-

As for the potential duplication, I saw that post, but it lacked any examples of using arguments created at the time of invocation. I wanted to know if the passed objects have no home in the invocation scope, if there was a way to dispose, if a dispose was necessary, or if a home must be given to properly handle disposal. It seems as though creating disposable objects with no ability to dispose of them in the creation scope is not recommended from what I gather here. –

Additionally, I can confirm that Martin's solution is effective, though doing the clones in the body of transform proved the most organized and clean. Only one of the clones needed to be disposed, as I used one as the object referenced in the return. Lastly, the clone was necessary, as any picturebox image refresh throws an exception if its image is in read bitlock,as is necessary for the transform.thanks for the help everyone

schulmaster
  • 413
  • 5
  • 16
  • Possible duplicate of [Who should call Dispose on IDisposable objects when passed into another object?](http://stackoverflow.com/questions/4085939/who-should-call-dispose-on-idisposable-objects-when-passed-into-another-object) – Cee McSharpface Feb 16 '17 at 17:37
  • It is not exactly obvious how those bitmaps could be harmed in the process. Clone() does not protect against that. But in general, yes, if you Clone() then you have to dispose the clone too. Or you'll leak when it is time to dispose A and B. Calling Dispose() inside Transform() with the code as posted is a grave mistake, it should always be obvious who the owner of the object is. If you really, *really* need to call Clone(), surely you don't since it doesn't do anything, then it should be done inside Transform(). And then you'd use the *using* statement. – Hans Passant Feb 16 '17 at 17:37
  • So let me quickly broach my Clone motive, and then maybe you can confirm it is unnecessary? The A and B Bitmaps are being displayed by an active PictureBox. If I were to bitlock the bitmaps in Transform, let alone make any changes, wont that invalidate the PictureBox reference, or at least change the picture box image as any changes are made in transform? Will control refresh be suspended in such a case? I guess I'm unsure on how tightly bound Windows Form objects are to any variables they're using for persistent GUI. – schulmaster Feb 16 '17 at 18:00
  • As for the potential duplication, I saw that post, but it lacked any examples of using arguments created at the time of invocation. I wanted to know if the passed objects have no home in the invocation scope, if there was a way to dispose, if a dispose was necessary, or if a home must be given to properly handle disposal. It seems as though creating disposable objects with no ability to dispose of them in the creation scope is not recommended from what I gather here. – schulmaster Feb 16 '17 at 18:07

1 Answers1

0

The garbage collector will not automatically call Dispose on objects implementing IDisposable. This will only happen if their classes have overriden the finalizer and ensured that Dispose is called. See the documentation here.

Usually the best approach is to create the instances of disposable objects in the using block.

using ( var bitmapA = ( Bitmap )A.Clone() )
using ( var bitmapB = ( Bitmap )B.Clone() )
{
    var newB = Transform( bitmapA, bitmapB );
} 
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • This successfully answered my question, but as it turns out I was asking a question that could lead to a lot of bad practices. still will accept. – schulmaster Mar 23 '17 at 04:59