|
The finalize method is needed to free unmanaged resources that the object may own (memory,handles, etc.). If these are never feed, there would be a memory leak. Not all objects have unmanaged resources, so many do not need a finalize method.
|
|
|
|
|
Cool answer graham.I am greatful to you. It would be great if you clear me one more thing. What do you mean by unmanaged resource(memory,...)here?
Thanks:
Rakesh
|
|
|
|
|
any resource not allocated by the CLR, and hence not garbage collectible. Some .Net framework classes interface to code written in c or c++ that allocates memory (or file handles, or network connection handles, etc.) outside of that managed by the CLR. All of these implement IDisposable and have finalize methods that insure that memory is released. User written classes that interact with COM objects or system dlls can also have unmanaged memory (received from the non-.Net objects used), and therefore need finalizers and should implement IDisposable to allow early (deterministic) release of those resources. This article[^] has a more detailed explanation of CLR meory management
|
|
|
|
|
Thanks a lot Graham. Please clear me one more general question:
Lets say that I have created an object and did some thing and then the application got crashed. Now will the garbage collector will reclaim the memory of this object and the resources it is pointing to?
Rakesh
|
|
|
|
|
Rob Graham wrote: The finalize method is needed to free unmanaged resources that the object may own (memory,handles, etc.). If these are never feed, there would be a memory leak. Not all objects have unmanaged resources, so many do not need a finalize method.
I'm a bit confused by this stuff at the moment. I read this article[^] and it seemed to suggest that a finaliser is required as a back up in case the person using your class does not call Dispose() when they are finished with the object.
For example, if I have a class A that has a reference to another dot net class B and attaches to events on B in the constructor where do I unattach from the event? If I put it in a Dispose() method and the user of the class does not call Dispose() on it what happens? The article above seemed to suggest that I should implement a Finalizer to handle this case
System.IO.Path.IsPathRooted() does not behave as I would expect
|
|
|
|
|
Josh Gray wrote: I read this article[^] and it seemed to suggest that a finaliser is required as a back up in case the person using your class does not call Dispose() when they are finished with the object.
That is correct. The preferred method of handling objects that uses unmanaged resources is to call the Dispose method. That way the programmer can control when the resources are being freed.
The finalizer is called by the garbage collector when the object is collected, if the Dispose method has not been called. The problem with this is that you can not predict when this will happen, and there isn't even any guarantee that the finalizer will ever be called. If there are a lot of objects in the finalizer queue when the program ends, it's not certain that all of them will be handled before the system decides that it takes too long to finalize all the objects, and just kills off the rest.
Josh Gray wrote: For example, if I have a class A that has a reference to another dot net class B and attaches to events on B in the constructor where do I unattach from the event? If I put it in a Dispose() method and the user of the class does not call Dispose() on it what happens? The article above seemed to suggest that I should implement a Finalizer to handle this case
It's good to have a finalizer as a fallback. That way the object will be disposed eventually (at least in most cases) if the programmer fails to call Dispose properly.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
You can also take a look at this article: http://www.codeproject.com/useritems/idisposable.asp[^]
The scenario you are describing is generally called "weak references" and isn't really supported in .NET. There are some blog posts about it, you can search on Google for them if you are interested.
Keep in mind that when you implement a finalizer, you are incurring extra costs to using your object so you should only implement a finalizer if you have a very good reason. Every object that has a finalizer is put on a finalization queue when it is allocated, even if the finalizer is not run. The finalizer will only be run when your object is garbage collected AND it has not already been disposed.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
rockyl wrote: I have got to know that when a finalizable object is unreachable then garbage collector removes its reference from finalizablr block and puts in freachable block and calls the finalize method and then frees the memory.
That will not happen. An object is not finalized when it's unreachable, it's just left untouched in memory until the next garbage collection.
If you want to free any resources, the class should implement IDisposable, so that you can call Dispose to tell the object that it won't be used any more.
At the garbage collection, if there is a finalizer in the class and GC.SuppressFinalize has not been called for the object, the object will be placed in the finalizing queue instead of being collected. A background thread is calling the finalizer for the objects in the queue, and after that the objects are up for collection.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Guffa wrote: At the garbage collection, if there is a finalizer in the class and GC.SuppressFinalize has not been called for the object, the object will be placed in the finalizing queue instead of being collected. A background thread is calling the finalizer for the objects in the queue, and after that the objects are up for collection.
The object is actually placed on the finalization queue when it is allocated. The GC.SupresFinalize call is important to tell the garbage collector that this object has been properly disposed and that it doesn't need to run the finalizer.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Scott Dorman wrote: The object is actually placed on the finalization queue when it is allocated.
I checked up on this, and you are correct.
I find it a bit misleading to call the entire list of finalizable objects a queue, though, as it's not really a queue. What do we call the queue in the queue, that contains the objects that actually are going to be finalized?
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
|
Scott Dorman wrote: Guffa wrote:
What do we call the queue in the queue, that contains the objects that actually are going to be finalized?
Very good question. I think what you are referring to is the "freachable" queue.
That is right. That is the real finalization queue.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
guys , if my understanding is right , is the following implementation correct for dispose finalise impementation?
public class MyComponent : IDisposable
{
.....
public void dispose()
{
CleanUp();
GC.SupressFinalize(this);
}
public void CleanUp
{
//clean up code here
}
~MyComponent
{
CleanUp();
}
}
Karam C Bose
|
|
|
|
|
Yes, that works.
Except the name of the method is Dispose , not dispose .
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
hi
i have a windows form which loads data from data base (sql server 2000)
after getting a unique value,, this form also shows an image stored in data base,, problem is when i update image means when i change the image and run update query all the fileds are changed except image,, it shows me every time the first image which i assigned to this record,,
i m changin image from open file dialog
thanks in advance
hello
|
|
|
|
|
Have you found a solution to the problem?
Some people have a memory and an attention span, you should try them out one day. - Jeremy Falcon
|
|
|
|
|
Hello friends,
What is the good method for authenticate username and passwordmethod for a webservice????
please guide me ....
Rgds
Nithin
|
|
|
|
|
New to Windows services. I've created one and have installed it successfully using the installutil.
Using the InstallUtil, do we have control of where the files get installed? Or am I misunderstanding it and the location where the files are existing is the location of the service files(exe, config, dll, etc...)
If that's the case then am I correct in thinking that all I need to do is xcopy the package of files to wherever I want then run installutil on them and all installutill really does is register and configure them in the system as a service?
Thanks in advance!
PhrankBooth
|
|
|
|
|
Hey, does anyone know how to deploy a VSTO addin for outlook 2007 for all users?
I've tried adding the registry entries to HKLM instead of HKCU and outlook 2007 just complains that it can't write to any HKLM registry entries.
I've heard that you can put them in HKU but i've not heard of anyone successfully doing it for all users current and newly created.
|
|
|
|
|
Where can i download VSTO version 2003(VS 2003)? I can not find the link to download. Only VSTO version 2005 is available in Microsoft's Site.
|
|
|
|
|
Hey all,
Can anyone tell me how I can modify the machine.config to add sections and keys (preferrably using vb.net)? I've been looking at system.configuration (.net 2.0) but I can't seem to wrap my head around it. Any good tutorials?
Thanks!
|
|
|
|
|
It's generally not a good idea to modify machine.config. If this file gets damaged/corrupted, then all of your .NET applications will have problems.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
hello programmers
this is ajay.
i have .net2003 installed on my server.
i m using microsoft windows2000 advanced server.
when i m trin to open .net.it gives me the error
"cannot find one or more components.please reinstall the application."
and when i m going to run the setup it asks for the deletetemp.exe....
wht to do?
thanks in advance
|
|
|
|
|
Hi,
I want to know how to create (control the generation of) my own public & private key pairs, is it correct to use the following codes? And what are valid parameters P, Q, Modulus, any sample codes?
----------------------------------------------------------------------
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
System.Security.Cryptography.RSAParameters parameters = new System.Security.Cryptography.RSAParameters();
parameters.P=???;
parameters.Modulus=???;
.............
...............
rsa.ImportParameters(parameters)
----------------------------------------------------------------------
Thanks & best regards,
Ed.
|
|
|
|