Link to home
Start Free TrialLog in
Avatar of ba272
ba272

asked on

Using the dispose() function

Greetings,

I'm not new to the concept of the Dispose() function since I'm a former C++ programmer.  But I think I've been too comfortable with C# which may have allowed memory leaks into my program.  I have never written or modified a Dispose function, and am not quite sure when I should use it or how to use it.

Could someone please give me suggestions about when I might need Dispose() and perhaps a snippet of code demostrating some typical uses of it?


Thanks,
Bob
SOLUTION
Avatar of cyberdevil67
cyberdevil67

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ba272
ba272

ASKER

What kinds of things have you personally used Dispose() for?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As Yurich has said graphics objects, opening classes that I have called and I know they need to be closed down, I usually write my own dispose for classes I write, so that I know that they will be closed down in a timely fashion.

But as Yurich has stated also its not mandatory, but it is perhaps best to do as best practice as the garbage collector provided it does its job will come along a release them down the track.

And I will also say the same thing setting an object to null is as far as I am aware too is killing the object.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> In all other cases (when not using unsafe C# code) the memory is ultimately
> freed by the garbage collector when your application is closed, also when
> writing and reading files.

And ofcourse when the reference count is null, wich is when the variable moves out of scope, therefore you don't need to set a local variable to null
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ba272

ASKER

Thanks for the tips.

Although I am not yet in a position to incorporate the "using" statement, this looks like the best one for the long haul.


Thanks,
Bob