This is why I am starting to loathe programming
-
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionI think I can avoid a downvote or two, on my way now. :cool:
-
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
SO WHERE IS YOUR UNMANAGED RESOURCE?
Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.peterchen wrote:
SO WHERE IS YOUR UNMANAGED RESOURCE?
if it was managed I'd know where it was, but my management of my resources (e.g. cash) is notoriously bad. I'd make a good accountant, but no great fund manager.
-
Daniel Grunwald wrote:
But it does lead to excessive resource usage and potentially even to resource exhaustion (where resource != memory).
That was my point too.
Daniel Grunwald wrote:
It's a bad idea to not dispose IDisposable objects.
I know you should, but you dont have to, unless you want it to be deterministic.
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionBy implementing IDisposable, the class creator explicitely told you to call
Dispose()
. If the documentation of a class said "You need to call Init() before using an instance of this class", would you reply with "Ah, I don#t feel like it today"?Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server. -
By implementing IDisposable, the class creator explicitely told you to call
Dispose()
. If the documentation of a class said "You need to call Init() before using an instance of this class", would you reply with "Ah, I don#t feel like it today"?Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.peterchen wrote:
the class creator explicitely told you to call Dispose().
No, he actually said: 'If you want to cleanup the resources immediately, then call Dispose(), but if you forget or dont want to, I'll do it anyways when the object finalizer is called.'
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
John Simmons / outlaw programmer wrote:
The GC also doesn't handle objects on the large heap.
It does, but in a different way. The large object heap can suffer from fragmentation that causes it to grow beyond what it is actually being used to store, but objects on the LOH still get collected, eventually. I can't remember the details at the moment (I think the framgentation problem is because LOH objects aren't shifted about... they can never move from one position in the LOH to another position in the LOH, because moving large objects is a costly operation), but the GC does "handle" the LOH objects in the sense that they get collected automatically.
yep, the difference is the move cost is high, therefore moves are avoided and that results in a fragmentation risk. But it isn't as black-and-white as it used to be; I have "frag demonstration code" that used to always work long ago (i.e. reach an intended out of memory situation easily), and more recently fails; I've never seen an improvement in LOH treadment documented though. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Editionleppie wrote:
I think I will get similar responses from here too though
I for one would set you straight if you were to publish such statements in one of CP's programming forums. :|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
peterchen wrote:
the class creator explicitely told you to call Dispose().
No, he actually said: 'If you want to cleanup the resources immediately, then call Dispose(), but if you forget or dont want to, I'll do it anyways when the object finalizer is called.'
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionHear hear! I implement IDisposable on a lot of classes that don't really need it, because the
using
pattern is so good at letting the reader clearly see the lifetime of an instance. I dislike finding that a class I want to use can't be used in ausing
statement. In my opinion,object
should have a virtual do-nothing Dispose method so that any class or struct can be used with theusing
statement. (We would therefore not need the IDisposable interface.) -
Hear hear! I implement IDisposable on a lot of classes that don't really need it, because the
using
pattern is so good at letting the reader clearly see the lifetime of an instance. I dislike finding that a class I want to use can't be used in ausing
statement. In my opinion,object
should have a virtual do-nothing Dispose method so that any class or struct can be used with theusing
statement. (We would therefore not need the IDisposable interface.)PIEBALDconsult wrote:
In my opinion, object should have a virtual do-nothing Dispose method so that any class or struct can be used with the using statement. (We would therefore not need the IDisposable interface.)
For everybody, not just piebald: it's been well over 1.5 years since I wrote C# production code, so take my words with a large dose of scepticism. I agree with Piebald here. Any gurus care to explain why this isn't the case?
Cheers, Vikram. (Got my troika of CCCs!)
-
peterchen wrote:
the class creator explicitely told you to call Dispose().
No, he actually said: 'If you want to cleanup the resources immediately, then call Dispose(), but if you forget or dont want to, I'll do it anyways when the object finalizer is called.'
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Editionleppie wrote:
when
You misspelled "if". I just don't like the default of "your" rule. "Call Dispose unless you know what you are doing" would be ok. "Don't call Dispose unless, umm, you feel like it" is not. Also, Having to call Dispose may affect code structure, which means figuring out later you have to call it may require major changes. [edit] as an example: Omitting Disposal of a resource holdign a file handle: You: "It's ok, we can have zillions of open file handles in windows". Me: "The file handle may remain open forever. Even if the user closed the file, he can't move or modify it in another program - or instance of this program - because we still keep the file handle open. It's one of those completely unecessary, insanely annoying bugs."
Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server. -
PIEBALDconsult wrote:
In my opinion, object should have a virtual do-nothing Dispose method so that any class or struct can be used with the using statement. (We would therefore not need the IDisposable interface.)
For everybody, not just piebald: it's been well over 1.5 years since I wrote C# production code, so take my words with a large dose of scepticism. I agree with Piebald here. Any gurus care to explain why this isn't the case?
Cheers, Vikram. (Got my troika of CCCs!)
C# is a garbage collected language. If you want explicit memory management for all object, use C++.
-
C# is a garbage collected language. If you want explicit memory management for all object, use C++.
Daniel Grunwald wrote:
C# is a garbage collected language
Exactly. It's not about memory management.
-
PIEBALDconsult wrote:
In my opinion, object should have a virtual do-nothing Dispose method so that any class or struct can be used with the using statement. (We would therefore not need the IDisposable interface.)
For everybody, not just piebald: it's been well over 1.5 years since I wrote C# production code, so take my words with a large dose of scepticism. I agree with Piebald here. Any gurus care to explain why this isn't the case?
Cheers, Vikram. (Got my troika of CCCs!)
Vikram A Punathambekar wrote:
Any gurus care to explain why this isn't the case?
Because the Dispose pattern was an afterthought.
Fight Big Government:
http://obamacareclassaction.com/
http://obamacaretruth.org/ -
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionYou are basically correct, leppie, but getting the great unwashed to understand that the dispose pattern is *not* a contract that *requires* the user to call it is like leading a horse to water. The dispose pattern is a contract with the GC, not with the user of the object.
Fight Big Government:
http://obamacareclassaction.com/
http://obamacaretruth.org/ -
C# is a garbage collected language. If you want explicit memory management for all object, use C++.
OK, let me put it in a slightly different way: If MS had implemented it Piebald's way, how would we be worse off?
Cheers, Vikram. (Got my troika of CCCs!)
-
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionI think that .NET makes it a lot easier for people to get into programming, you don't need to know a lot about what it does, you can put buttons down on forms or web pages and wire up some code and you're good to go. Unfortunately, we they become actual programmers, and they don't understand some of the core functionality of what they're working with. The attitude of "you can't create memory leaks in .NET" is really common. It's oddly apt that this week I've been tasked with finding out why a couple of .NET websites we have consume up to 100mb a page load(eventually causing out of memory exceptions on customer servers) and I'm finding so many interesting discussions on IDisposable and GC at the same time! I've been busily going over objects, implementing finalize and Dispose, trying to figure out what is going wrong and it doesn't seem to make the slightest bit of difference whether I explicitly call Dispose or whether I just leave it be, although I think I have to get a better understanding of what is truly happening to stop some of the objects actually being collected. I like to think that implementing an interface shows my intent. So I don't quite agree with the idea of making object support Dispose by default. I like the using statement, if I create write a class that has a db connection or reads files etc, I'll always make it disposable and in my code wrap it in using. I've been advising the none .NET developers at work (starting to develop .NET applications) to work like this also.
modified on Saturday, May 29, 2010 10:03 AM
-
leppie wrote:
when
You misspelled "if". I just don't like the default of "your" rule. "Call Dispose unless you know what you are doing" would be ok. "Don't call Dispose unless, umm, you feel like it" is not. Also, Having to call Dispose may affect code structure, which means figuring out later you have to call it may require major changes. [edit] as an example: Omitting Disposal of a resource holdign a file handle: You: "It's ok, we can have zillions of open file handles in windows". Me: "The file handle may remain open forever. Even if the user closed the file, he can't move or modify it in another program - or instance of this program - because we still keep the file handle open. It's one of those completely unecessary, insanely annoying bugs."
Agh! Reality! My Archnemesis![^]
| FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server.peterchen wrote:
You misspelled "if".
No, I meant 'when'.
Dispose
should always be called from a finalizer.peterchen wrote:
Omitting Disposal of a resource holdign a file handle:
Again, you should call
Close
if you want to release the file handle.Dispose
willClose
the file handle if still open. Not callingDispose
afterClose
will not cause a resource leak. Please look my example on SO again. What you are almost saying isIDisposable
objects stay alive regardless, which is not true. If the programmer makes this object non-GC'able, obviously the GC cant do it's job. Stupid code leads to stupid bugs. You might want to ask why your object is not being GC'ed instead. Like why it is being assigned to an instance variable where a local variable would suffice, etc.xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
I think that .NET makes it a lot easier for people to get into programming, you don't need to know a lot about what it does, you can put buttons down on forms or web pages and wire up some code and you're good to go. Unfortunately, we they become actual programmers, and they don't understand some of the core functionality of what they're working with. The attitude of "you can't create memory leaks in .NET" is really common. It's oddly apt that this week I've been tasked with finding out why a couple of .NET websites we have consume up to 100mb a page load(eventually causing out of memory exceptions on customer servers) and I'm finding so many interesting discussions on IDisposable and GC at the same time! I've been busily going over objects, implementing finalize and Dispose, trying to figure out what is going wrong and it doesn't seem to make the slightest bit of difference whether I explicitly call Dispose or whether I just leave it be, although I think I have to get a better understanding of what is truly happening to stop some of the objects actually being collected. I like to think that implementing an interface shows my intent. So I don't quite agree with the idea of making object support Dispose by default. I like the using statement, if I create write a class that has a db connection or reads files etc, I'll always make it disposable and in my code wrap it in using. I've been advising the none .NET developers at work (starting to develop .NET applications) to work like this also.
modified on Saturday, May 29, 2010 10:03 AM
hammerstein05 wrote:
It's oddly apt that this week I've been tasked with finding out why a couple of .NET websites we have consume up to 100mb a page load(eventually causing out of memory exceptions on customer servers) and I'm finding so many interesting discussions on IDisposable and GC at the same time!
I am willing to bet it aint resource/memory leaks, but rather stupid code causing this. Like having 200 controls on a page, and tonnes of event handlers initialized on every request. But anyways, unless you see a 100mb per page load increase, that is not what is being used.
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
It's like the blind leading the blind.... http://stackoverflow.com/questions/2926869/c-do-you-need-to-dispose-of-objects-and-set-them-to-null/2926877#2926877[^] I think I will get similar responses from here too though :sigh:
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth EditionI've become convinced over the years that the rules for you "managed" folks are much more complicated than for us "unsafe" folks. Deterministic destruction is so much simpler IMO. Throw in a good memory leak detector and debug assertion checks and you've got it made.