Skip to main content
9 events
when toggle format what by license comment
Aug 16, 2013 at 1:07 comment added Jerry Coffin @Andrew: Let me summarize: in C++, you get uniform, semi-automatic management of all resources. In C#, you get completely automatic management of memory -- and completely manual management of everything else. There's no real changing it, so your only real question is not "how do I fix this?", only "how do I get used to this?"
Aug 15, 2013 at 12:36 comment added Brian @RobertHarvey: Hence my answer states, "it's not necessary for managed resources."
Aug 14, 2013 at 23:02 comment added Telastyn @Andrew - not at all. I would make the same recommendation in a C++ program, though it's at least more common (for objects that are not files) in C++.
Aug 14, 2013 at 22:43 comment added Robert Harvey @Andrew: No, it doesn't mean that. What it does mean is that you should not have to concern yourself at all with object lifetime, unless you require near real-time capabilities (difficult to achieve on a platform like Windows which doesn't offer such performance guarantees), in which case there are techniques like object pooling and reuse that can mitigate the problem of garbage collection occurring when it is most inconvenient.
Aug 14, 2013 at 21:57 comment added Andrew @Telastyn Does it mean that giving away ownership over a managed resource is a bigger problem in C# than in C++? Quite surprising.
Aug 14, 2013 at 21:45 comment added Telastyn @Andrew - In general, you should not do that. You should instead tell the thread how to get the file and let it have ownership of the lifetime.
Aug 14, 2013 at 21:37 comment added svick @Andrew What you describe seem to imply transfer of ownership, so now the second thread is responsible for Disposing() the file and should probably use using.
Aug 14, 2013 at 21:19 comment added Andrew RAII is my biggest trouble indeed. Say I have a resource (say a file) that is created by one thread and given away to another. How can I make sure that it is disposed at a certain time when that thread does not need it anymore? Of course, I could call Dispose but then it looks way uglier than shared pointers in C++. There's nothing of RAII in it.
Aug 14, 2013 at 21:09 history answered Brian CC BY-SA 3.0