Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to release object stored in property?
Message
From
05/05/2009 10:50:20
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01397173
Message ID:
01397970
Views:
37
>>>>>Hi,
>>>>>I initialize object to private vairable via
>>>>>
>>>>>public class MyBase
>>>>>{
>>>>>private MyClass _myClass = new MyClass();
>>>>>}
>>>>>
>>>>>I found that it caused memory leak as it is not released. May I know how should I release it once myBase is released?
>>>>>
>>>>>Thank you
>>>>
>>>>Take a look at these references
>>>>
>>>>http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae
>>>>http://lyontamers.com/blogs/jimlyon/archive/2008/08/29/garbage-collection-finalizers-and-dispose-what-every-c-programmer-should-know.aspx
>>>
>>>Thank you for the links. I sort of did have the impression that the .NET garbage collector takes care of things for me and I only need to implement Dispose() for expensive resources. I think I know better now.
>>
>>I am working through this myself. FXCop recommends implementing IDisposable for my classes that have class fields which are assigned objects that implement IDisposable.
>
>Hi,
>At first glance that makes sense - and I've seen the same advice elsewhere.
>But taken to its logical conclusion wouldn't that mean that the implementing of IDisposable would need to 'ripple up' through every class that, however indirectly, accessed a .NET class implementing IDisposable.
>And even then the idea seems questionable. If the innermost IDisposable is not going to run until the highest level one executes then where's the benefit.
>
>Viv

I'm not sure I see the benefit either. The issue arose as we were converting our app from 1.1 to 3.5. We have never observed a memory leak from NOT implementing IDisposable on our classes. A couple of our classes have a datatable field. FXCop flagged this field with the "should implement IDisposable...". However, the 3.5 documentation does not list IDisposable as an interface that the datatable class implements. Since we have not observed any memory leaks we are leaving things as they are for now.
Previous
Reply
Map
View

Click here to load this message in the networking platform