How Do I Dispose of an object

Posted on 2009-04-27
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 74

    Assisted Solution

    by:käµfm³d 👽
    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

    > 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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
    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.…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now