Is this OK or do I need a Dispose

A

active

Private Sub AdjustBitmap()

If b1 Is Nothing Then Exit Sub

Dim b = New Drawing.Bitmap(picData.Width, picData.Height)

Dim g As Graphics = Graphics.FromImage(b)

g.DrawImage(b1, 0, 0)

b1 = b

g.Dispose()

End Sub

Is the above OK? I think I need to Dispose the old Bitmap but don't know
how.

Do I need to Dispose it or will garbage collection take care of it?

If I do, how. Would the following be sufficient?

At the top add bb=b1

At the bottom add bb.Dispose



As always thanks for any helpful info,

Cal
 
A

Armin Zingler

active said:
Private Sub AdjustBitmap()

If b1 Is Nothing Then Exit Sub

Why exit sub?
Dim b = New Drawing.Bitmap(picData.Width, picData.Height)

Dim b as bitmap = new ....

Dim g As Graphics = Graphics.FromImage(b)

g.DrawImage(b1, 0, 0)

if not b1 is nothing then
b1.dispose
end if
b1 = b

g.Dispose()

End Sub

Is the above OK? I think I need to Dispose the old Bitmap but don't
know how.

Do I need to Dispose it or will garbage collection take care of
it?

Yes, you need to dispose it. Yes, GC will take care of it, but not
immediatelly.
 
H

Herfried K. Wagner [MVP]

* " active said:
Private Sub AdjustBitmap()

If b1 Is Nothing Then Exit Sub

Dim b = New Drawing.Bitmap(picData.Width, picData.Height)

Dim g As Graphics = Graphics.FromImage(b)

g.DrawImage(b1, 0, 0)

b1 = b

g.Dispose()

End Sub

Is the above OK? I think I need to Dispose the old Bitmap but don't know
how.

You won't need to dispose the "old bitmap" if 'b1' is pointing to
'Nothing'. In this case, no old bitmap exists.
Do I need to Dispose it or will garbage collection take care of it?

There is nothing to dispose. You will have to dispose the bitmap if the
code reads something like that:

\\\
If b1 Is Nothing Then
...
Else
...
b1.Dispose()
b1 = b
...
End If
///
 
A

active

I had tried to do this but taught I needed to redeclare b1 after I did the
Dispose (I see now that does not make sense) and the conpiler complained.

The Dispose actually effects the object referenced by b1 (not b1), right?

Thanks
Cal
 
H

Herfried K. Wagner [MVP]

* " active said:
The Dispose actually effects the object referenced by b1 (not b1), right?

Yes. 'b1' is only holding a /reference/ to an instance of 'Bitmap'.
 
C

Cor

Hi Herfried,

In my opinion this sample is very bad programming in the given situation
\\\
If b1 Is Nothing Then
...
Else
...
b1.Dispose()
b1 = b
...
End If
///

Cor
 
C

Cor

Herfried,

Armin mentioned it,

But in general, I find that you never should test for something in a sub or
function that could be tested before it was delivered to the sub. It looks
maybe nice, but than it becomes endles.

But I do not like the sentences at all, there is to much global for me.
That is not a problem as a newbie does that, but when you do that I find it
bad programming.

But just my opinion
However, you know I always say let everybody do it as he likes.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
Armin mentioned it,

Armin checked if the varaible is 'Nothing' in his reply.
But in general, I find that you never should test for something in a sub or
function that could be tested before it was delivered to the sub. It looks
maybe nice, but than it becomes endles.

There is no general way, sometimes it makes sense, sometimes not.
 
C

Cor

Herfried,

You ask maybe why I did write this,

I once did bring back program from about 200 pages of code to 2 pages
(initialy it was not mine of course).
It was not the only thing, but a big part of the code was completly filled
with this kind of things testing after testing after testing after testing.

And what was it testing in most cases nothing (but not in the meaning we use
it here).

The performance was afterward 4 times higher.
 
H

Herfried K. Wagner [MVP]

* "Cor said:
You ask maybe why I did write this,

I once did bring back program from about 200 pages of code to 2 pages
(initialy it was not mine of course).
It was not the only thing, but a big part of the code was completly filled
with this kind of things testing after testing after testing after testing.

And what was it testing in most cases nothing (but not in the meaning we use
it here).

The performance was afterward 4 times higher.

Defensive programming has advantages, but as your sample showed, it has
some disadvantages too. When writing a component providing some
classes, it may be "ugly" to have a 'NullReferenceException' thrown.
 
C

Cor

Hi Herfried,

I was waiting for this 2 messages before, than it is a function or a
property that returns Something or Nothing.
(And therefore in that case allowed in my eyes to test the provided value or
object)

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
I was waiting for this 2 messages before, than it is a function or a
property that returns Something or Nothing.
(And therefore in that case allowed in my eyes to test the provided value or
object)

When returning? Mhm... I don't get the point.
 
C

Cor

Hi Herfried,

You where talking about a component where this function in this thread was a
part of.

Then you have to do something with something you gets from the user of that
function.

"Not with you of course because, probably you will have a global value
"Herfried" or something always in a module and that you fill before you
start the function and reads it again when the function is done. But some
people dont like that kind of programing." :))))

When you give something to a function you want to have something back. Some
people calling that what the program is returning. And that has to be
something or nothing.

Or are you just prickling me?

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
"Not with you of course because, probably you will have a global value
"Herfried" or something always in a module and that you fill before you
start the function and reads it again when the function is done. But some
people dont like that kind of programing." :))))

When you give something to a function you want to have something back. Some
people calling that what the program is returning. And that has to be
something or nothing.

Thanks. Everything is clear to me now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top