Click here to Skip to main content
15,898,134 members
Home / Discussions / C#
   

C#

 
GeneralRe: dispose wrapped resource Pin
Simon P Stevens16-Apr-08 1:21
Simon P Stevens16-Apr-08 1:21 
GeneralRe: dispose wrapped resource Pin
George_George16-Apr-08 1:55
George_George16-Apr-08 1:55 
GeneralRe: dispose wrapped resource Pin
Simon P Stevens16-Apr-08 4:18
Simon P Stevens16-Apr-08 4:18 
GeneralRe: dispose wrapped resource Pin
George_George16-Apr-08 4:23
George_George16-Apr-08 4:23 
GeneralRe: dispose wrapped resource Pin
led mike15-Apr-08 5:13
led mike15-Apr-08 5:13 
GeneralRe: dispose wrapped resource [modified] Pin
Simon P Stevens15-Apr-08 7:09
Simon P Stevens15-Apr-08 7:09 
GeneralRe: dispose wrapped resource Pin
George_George15-Apr-08 23:28
George_George15-Apr-08 23:28 
GeneralRe: dispose wrapped resource Pin
Simon P Stevens16-Apr-08 1:32
Simon P Stevens16-Apr-08 1:32 
Not quite.

If you do absoutly nothing at all, no dispose method, no finalizer, nothing. Then what will happen is that all objects that needed disposing of (like the component object in your example) will eventually get cleaned up by the GC which will call the finalizers. This isn't great though. Objects that have to be finalized have to go onto the finalizer queue, which means they stay in memory longer (They survive one GC generation longer) so your memory usage is higher, and resources aren't freed up when they're finished with, it may be quite a while before the finalizer gets called. But yes, eventually all managed resources will get cleaned up by the GC finalizer thread. And thus, any native resources wrapped by the managed classes will also get cleaned up.

However, if you directly use a native resource yourself there is nothing to clean it up. If you use a native resource directly, you must make sure you clean it up! if you don't, you have a memory leak.

The handy thing is, if you implement the dispose pattern on your class, then you are wrapping the native resouce yourself, so users of your class don't have to worry about it, so if users of your class forget to call dispose, you don't have to worry because your classes finalizer will eventually do the work.


Simon

GeneralRe: dispose wrapped resource Pin
George_George16-Apr-08 1:59
George_George16-Apr-08 1:59 
GeneralRe: dispose wrapped resource Pin
Zoltan Balazs16-Apr-08 2:41
Zoltan Balazs16-Apr-08 2:41 
GeneralRe: dispose wrapped resource Pin
George_George16-Apr-08 2:46
George_George16-Apr-08 2:46 
GeneralRe: dispose wrapped resource Pin
Zoltan Balazs15-Apr-08 7:44
Zoltan Balazs15-Apr-08 7:44 
GeneralRe: dispose wrapped resource Pin
George_George15-Apr-08 23:31
George_George15-Apr-08 23:31 
GeneralRe: dispose wrapped resource Pin
Zoltan Balazs16-Apr-08 2:39
Zoltan Balazs16-Apr-08 2:39 
GeneralRe: dispose wrapped resource Pin
George_George16-Apr-08 2:44
George_George16-Apr-08 2:44 
GeneralCoding Style Question Pin
Waleed Eissa15-Apr-08 4:40
Waleed Eissa15-Apr-08 4:40 
GeneralRe: Coding Style Question Pin
led mike15-Apr-08 4:59
led mike15-Apr-08 4:59 
GeneralRe: Coding Style Question Pin
leppie15-Apr-08 5:09
leppie15-Apr-08 5:09 
GeneralRe: Coding Style Question Pin
Waleed Eissa15-Apr-08 5:12
Waleed Eissa15-Apr-08 5:12 
GeneralRe: Coding Style Question Pin
Dan Neely15-Apr-08 5:26
Dan Neely15-Apr-08 5:26 
GeneralRe: Coding Style Question Pin
Waleed Eissa15-Apr-08 5:38
Waleed Eissa15-Apr-08 5:38 
GeneralRe: Coding Style Question Pin
Dan Neely15-Apr-08 7:00
Dan Neely15-Apr-08 7:00 
GeneralRe: Coding Style Question Pin
K.L.K15-Apr-08 8:45
K.L.K15-Apr-08 8:45 
GeneralRe: Coding Style Question Pin
led mike15-Apr-08 7:25
led mike15-Apr-08 7:25 
GeneralRe: Coding Style Question Pin
Waleed Eissa15-Apr-08 5:10
Waleed Eissa15-Apr-08 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.