[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How Do I Dispose of an object

Posted on 2009-04-27
Medium Priority
Last Modified: 2013-11-25
Im implementing copy and paste functionality in my windows MDI application.
Im using Visual Studio 2008 and programming in Visual C#

In the child window I have a "copy" menu item which does this.

((mainWin)(this.MdiParent)).copiedResults = new List<int>(theResults);

It basically copies a list of ints called "theResults" to a variable in the parent window.

Every time the user selects "copy" then the value of "theResults" should be copied to "copiedResults" in the parent window.

But every time the following is executed

((mainWin)(this.MdiParent)).copiedResults = new List<int>(theResults);

does this not create a new object? Wont I have lots of objects floating about eventually. Should I be doing something like;

if(((mainWin)(this.MdiParent)).copiedResults != null)
((mainWin)(this.MdiParent)).copiedResults = new List<int>(theResults);

but Dispose(((mainWin)(this.MdiParent)).copiedResults);

throws an error

Any ideas how I can make this work?
Question by:rangers99
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 160 total points
ID: 24241366
That's the beauty of managed languages--once there is no longer a reference to an object, the object is automatically cleaned up via the garbage collector. The only time you need to worry about cleaning up objects yourself is when you are dealing with unmanaged objects, such as opening files and I believe COM objects.
LVL 39

Accepted Solution

abel earned 240 total points
ID: 24241377
> does this not create a new object?.

yes, the "new" instruction creates new object. But that happens all the time, and you should not worry about it. Object creation can be very cheap (depending on what's in the constructor of the object) and you should not worry about it.

Why you need a copy of the results, I don't know, but it is certainly not wrong. If you need to be safe on memory requirements, you can consider using a class variable on mainWin, which you can then set like this:

mainWin.currentResults = bla;

and that will be available to everybody. If you have only one mainWin ever, this is a simpler solution.

The Dispose part is not something that you need here. Dispose is meant for classes that implement IDispose, which is a story about resources that may stay around even after object destruction. Examples of that are streams and filewriters, for instance.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question