• 0

C# memory leak


Question

I cant seem to figure out why my C# program isn't disposing objects.

I dont want to post all the code as i doubt anyone would take the time to look through it... so here is a quick summary.

I have a navigation bar ( like vista ) with a stack (navigation_Objects) behind the scenes to store controls that the user click on.

		private Control[] navigation_Objects = new Control[15];

		public void push(Control pushedObject, String navigationText)
		{
			// create divider image
			PictureBox divider = new PictureBox();
			divider.Image = Properties.Resources.divider;
			divider.Size = new System.Drawing.Size(26, 19);
			divider.SizeMode = PictureBoxSizeMode.Normal;
			divider.Margin = new System.Windows.Forms.Padding(0);

			Label topicLabel = new Label();

			// store navigation object reference in the tag field
			topicLabel.Tag = this.returnNextEmptyArraySpace().ToString();
			navigation_Objects[this.returnNextEmptyArraySpace()] = pushedObject;

			topicLabel.Text = navigationText;
			topicLabel.Click += new System.EventHandler(this.topic_Clicked);
			topicLabel.Cursor = System.Windows.Forms.Cursors.Hand;
			topicLabel.AutoSize = true;

			navigationPanel.Controls.Add(divider);
			navigationPanel.Controls.Add(topicLabel);
		}

Once the user clicks on an previous topic, all of the items on the stack after the current item are all set to null.

(That removes all references to the object, right?)

		private void topic_Clicked(object sender, EventArgs e)
		{
			Label topicLabel = (Label)sender;

			this.popBackTo(topicLabel.Text);
			parentForm.setMainPanel(navigation_Objects[int.Parse(topicLabel.Tag.ToString())]);

			this.deleteOldObjects(int.Parse(topicLabel.Tag.ToString()) + 1);
		}


		private void deleteOldObjects(int deleteFromPosition)
		{
			while (navigation_Objects[deleteFromPosition] != null)
			{
				navigation_Objects[deleteFromPosition] = null;
				deleteFromPosition++;
			}
		}

and the content of the main panel is set to the control stored in the stack

		public void setMainPanel(Control newObject)
		{
			mainPanel.Controls.Clear();
			mainPanel.Controls.Add(newObject);

		}

This seems to work fine but im sure i have a memory leak (think thats the correct term as objects dont seem to be deleted).

For example...

I navigate to Home > Topic 1 > Game

The game appears and the background music start.

When i navigate back to Home > Topic 1

I can still hear the game in the background... So it would seem that the game control object was not disposed...

Hope im not being too vague

Any idea how i can fix this problem...

Thanks

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

In theory, you don't know when an object will be garbage collected. Even though you set all references to it to null, the object may continue to exist in heap memory for an undefined period of time.

Objects that contain critical ressources which must be deterministically disposed of should implement the Disposable pattern.

Link to comment
Share on other sites

  • 0

Thanks for that link... Now im just wondering if i am using the method correctly.

I created a new class called "DisposableResource" and pass in the object i want to distroy... Seems to work... some of the time.

I must not be handling object references well throughout the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EndaProject
{
	public class DisposableResource : IDisposable
	{

		private Control deleteControl;
		private bool disposed;

		public DisposableResource(Control controlDelete)
		{
			deleteControl = controlDelete;
			disposed = false;
		}


		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!disposed)
			{
				if (disposing)
				{
					if (deleteControl != null)
						deleteControl.Dispose();
				}

				// Indicate that the instance has been disposed.
				deleteControl = null;
				disposed = true;
			}
		}
	}

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.