473,796 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does the Finalize event of a class fire?

VB 2005.

I have the following code in a Sub.

Dim oFred As SillyClass
oFred = New SillyClass
oFred.Gloop()
oFred = Nothing

Exit Sub

The Finalize code in the class does not fire until the .exe is closed.
I expected it to fire at either:-
- the point at which oFred is set to Nothing, or
- the point at which we exit from the Sub, and variable oFred goes out of
scope.

What are the rules governing when the Finalize code runs?

Thanks

Barry
Jun 27 '08
15 7423
On 2008-06-14, Cor Ligthert[MVP] <no************ @planet.nlwrote :
You mistunderstood my message however, something you added is something I
disagree with you.

In my idea you should use dispose as it is needed and were it is made for,
not because that is there given because it is inheritted by component.
But, unless you dive in with reflector, or it is explicitly documented, how do
you know that it isn't necessary? It is definately necessary for many objects
that do inherit from component - how do you distinguish? And what if the
implementation of said object changes in the future?

Sure, I know how to find out if the call is necessary or not - but I
prefere these sort of things to be a black box. I don't really want to know
it's implementation details. And in general, a few clock cycles to make the
call isn't going to noticibly impact the performance.
I have too often read the sentence, it is a method in the class so it is
best practise to use it. I get the idea you write that in a way.

It is for me not good practise to use an method, just because that it is in
a class. I don't use ToString as I need to count two integers.

Dim a as integer = 1
Dim b as integer = 1
Dim C as integer = Cint(a.ToString ) + Cint(b.ToString )
That's sort of stretching the point, Cor. I am sure there aren't many people
who would do such a thing. But, I think the call to dispose falls in the
category of defensive coding and OOP's principle of information hiding...
In that way as is written so often wrong about dispose this above would be
also best practise. For me it's not.
Nobody is advocating calling methods just because they exist.
There is in my idea nothing wrong with doing someting twice as that makes
the total proces shorter, especially as that is done at the best managed
moment.
I'm not sure I get you here? Not calling dispose can negatively affect the
performance of the gc, because it normally has to make two passes to remove and
object that implements idispose if dispose insn't called... That has nothing
to do with being disposable of course - but the fact that most objects that
implement IDisposable also include a finalizer. How is that in anyway a good
thing?
(This including that the "using" automaticly executes dispose, what makes
the using at least more simple to describe)
Using is the same as calling dispose, yes. And I use it :)
Which does not mean that I don't use dispose, I always use that for drawing
objects, dialogforms, etc.

Just my idea.
With which, I respectfully disagree :)

--
Tom Shelton
Jun 27 '08 #11
Tom,
>
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway a
good
thing?
I have seen that the GC works as the video processor is doing its job, I am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor

Jun 27 '08 #12
On Jun 14, 2:30 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
wrote:
Tom,
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway a
good
thing?

I have seen that the GC works as the video processor is doing its job, I am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong. I've
also seen this topic enough to know it doesn't really matter what
arguments we give, you aren't going to change your opinions. For this
reason I encourage readers of this thread who want the "full" debate
to do some searching in the archives.

However, just to restate my opinion, I believe you you should use
IDisposable every time unless you are absolutely sure that there is no
reason to call it (such as Cor's example of inherited Components).
This has nothing to do with calling the method simply because it
exists (such as Cor's attempt at saying this through his "ToString"
sample), it has to do with calling the method since the developer's
say it is necessary (even though sometimes it is not). Personnally, I
would not allow code to go to production that does not use the Dispose
functionality where it is needed (db transactions, streams, graphics,
etc). This to me is a worse "no-no" than writing code without having
Option Strict turned on.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #13
Seth,

What do you want to say with your complete message. You write exactly how I
think and always write about it.

Even in this thread I have explicitly given situations where it should be
used.

But that as well as you create your own classes where IDispose is not
implemented and unmanaged resources are used.

But not as you always tells to dispose things as datatables, datasets or a
Sqlconnection (the latter has the dispose implemented in the close).

So what do you mean with this cheap agusing message what tells nothing then
some demagogie.
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong
Cor
"rowe_newsgroup s" <ro********@yah oo.comschreef in bericht
news:8f******** *************** ***********@c65 g2000hsa.google groups.com...
On Jun 14, 2:30 pm, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
wrote:
>Tom,
I'm not sure I get you here? Not calling dispose can negatively affect
the
performance of the gc, because it normally has to make two passes to
remove and
object that implements idispose if dispose insn't called... That has
nothing
to do with being disposable of course - but the fact that most objects
that
implement IDisposable also include a finalizer. How is that in anyway
a
good
thing?

I have seen that the GC works as the video processor is doing its job, I
am
not completely sure of that, but as it is like that, it is in fact null
time, while your dispose takes needed proces time.

I agree that this is not for non video processes. I am working at one at
the
moment, it runs at night, nobody cares if it takes 2 or 6 hours.

Cor

I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong. I've
also seen this topic enough to know it doesn't really matter what
arguments we give, you aren't going to change your opinions. For this
reason I encourage readers of this thread who want the "full" debate
to do some searching in the archives.

However, just to restate my opinion, I believe you you should use
IDisposable every time unless you are absolutely sure that there is no
reason to call it (such as Cor's example of inherited Components).
This has nothing to do with calling the method simply because it
exists (such as Cor's attempt at saying this through his "ToString"
sample), it has to do with calling the method since the developer's
say it is necessary (even though sometimes it is not). Personnally, I
would not allow code to go to production that does not use the Dispose
functionality where it is needed (db transactions, streams, graphics,
etc). This to me is a worse "no-no" than writing code without having
Option Strict turned on.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #14
I'll do my best to answer your questions Cor, though I am having a bit
of trouble understanding some of what you are saying here.
What do you want to say with your complete message. You write exactly how I
think and always write about it.
The purpose of my message was to encourage readers to find the
previous threads where we have been over this.
Even in this thread I have explicitly given situations where it should be
used.
Again, many of the areas where we differ in opinion is whether or not
you should call Dispose if you do not know whether it is needed. We've
covered this in other threads, and so as to not waste time here I will
not go over it again.
But that as well as you create your own classes where IDispose is not
implemented and unmanaged resources are used.
Are you saying that I never reference unmanaged code in my projects?
If so you are a fool for assuming that I don't do this. And I assure
you that when I reference unmanaged code I implement IDisposable for
cleanup.
But not as you always tells to dispose things as datatables, datasets or a
Sqlconnection (the latter has the dispose implemented in the close).
Close and Dispose methods do different things. Again so as to not
waste time please read the following thread:

http://groups.google.com/group/micro...7e0a66a380a387
So what do you mean with this cheap agusing message what tells nothing then
some demagogie.
I have no idea what demagogie is, but I'll attempt to answer what I
think you are asking.
I'm not sure how many times we've been through this conversation on
the newsgroups, but I'm sure we've covered all your arguments and
proved them to be rooted in misinformation or just plain wrong
Not sure how I could get any clearer. You have listed many arguments
against using the IDisposable pattern as it was intended and I and
many others have shown you in previous threads where you are mistaken,
however I don't think you have ever changed your opinion. Remember
though Cor, just because you don't understand why you are wrong or you
refuse to admit you are wrong does not mean you aren't still wrong.

Some of the many things you have said which I believe are blatantly
wrong are:

That calling Dispose is the same as calling ToString()
The Garbage Collector cleans up any resource better without any
interaction by the developer
The Garbage Collector runs during "idle" times and never affects
performance
Dispose should be avoided unless you absolutely know it must be called
More I'm sure I've forgotten

Again, I'm not going to list rebuttals here, I encourage readers to
use the archives and find the previous threads and form their own
opinions. As for me, I'm dropping this thread.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #15
Seth,

snip
http://groups.google.com/group/micro...7e0a66a380a387
snip

In my idea you really should take some more time before you write something.
This was the situation in 2002 were was told that the connection dispose was
overloaded with the removal of the connection from the connectionpool. In
fact that has nothing to do with the dispose. To Implement Dispose to remove
*unmaneged resources* be done by this.

http://msdn.microsoft.com/en-us/libr...tw(VS.85).aspx

Luckely is IDisposable standard implemented in th by the designer created
templates like Form and Component where that is needed.

I have the ides, that what you are talking about all the time is the in my
idea the overloaded Dispose (boolean) method, which is strongly adviced not
to use on its own.

Things as DataTables don't need dispose because it is only a bunch in memory
objects, which have not any unmanaged resource.

By the way, the by you refered message is more times corrected by Pablo
Castro. Here just one I found. Maybe you could use Google in future and not
come direct with something you found from 2002, while giving the idea in
your message that I had to do with it while I was not.

http://groups.google.com/group/micro...11c0671640ad38

Maybe you can use Google to find something Jay B. Harlow wrote, he did made
before he wrote that forever the same discussions like you with me about
dispose.

Then he did some investigation in it and wrote the article. In that article
Jay B. made more clear where dispose should be used. His article was mainly
covering the aspects as I write about dispose. But he added some for me new
aspects, which I use when I write about dispose, by instance the dispose of
the dialogs, which I did but I did not know why before his article.

Cor


Jun 27 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3130
by: Dennis C. Drumm | last post by:
I have a class derived from a SortedList called SystemList that contains a list of objects indexed with a string value. The definition of the objects contained in the SortedList have a boolean field and an event that is fired if the boolean field value changes. The derived SortedList class has an override for the Add method. The Add method adds an event handler for the object's event for each new item added to the list. In the...
4
3909
by: Anatoly | last post by:
Put any control on web page. create Init event for ths control. Write Response.Write("here") inside this event. Compile\build\run. I never saw "here" string appear on web page. Why???
4
1569
by: ryu | last post by:
I have a aspx page that loads a ascx file ie Web Control. My question is when does the postback of the web control occur? Is it when it is called via LoadControl? Or after the calling page's postback or is it random?
20
7523
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable, but this seems to rely on a call from the application, e.g. MyClass.Dispose()
0
1083
by: Charles Law | last post by:
Is there a way to get the MouseEnter event to fire for a user control that inherits from UpDownBase? The event is hidden, and does not fire when the mouse enters the control. TIA Charles
1
5143
by: the friendly display name | last post by:
I am using .net 1.1 In the global.asax.cs file, there is this entry: protected void Application_AuthenticateRequest(Object sender, EventArgs e) as far as I know, it is wired with the FormsAuthentication_OnAuthenticate event.
1
1492
by: John Dalberg | last post by:
What causes a server event to fire when something happens on the client? Say a user changed the gridview's page index, the server side PageIndexChanged event is fires. What makes PageIndexChanged fires? Is it a field in the viewstate? Is it a value in the forms collections? I am trying to understand the mechanism of communication between the browser and the server. John Dalberg
0
1610
by: manywolf | last post by:
I have an aspx page that fires the page load event twice for every load. I tried every fix that was suggested in all the posts on this and other forums. None changed the behavior. After one post that suggested they had an img tag with src="", I decided to look for instances of "src" and one by one start removing that code to see if it changed anything. Below is the one that, when I removed it, although it broke the flash menus and header, solved...
7
4764
by: ghd | last post by:
In Windows XP when does the JVM start (JRE version 1.4 and higher)? And when does it halt? Does the JVM start when we launch a java application (or by executing java classfile)? And does the JVM halt when the all the java applications on the system end? According to the documentation, the JVM halts in two situations: 1. when you terminate the java application by Ctrl+C and 2. when the 'exit' method of the System class is called. But one of my...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.