c# memLeak?

Post Reply
timWP01
Posts: 4
Joined: Mon Jul 27, 2009 2:38 pm

c# memLeak?

Post by timWP01 »

hi,
was wondering if anybody has found the same issue as me....
basically whenever i compile and run a very simple c# application, which just displays the x and y of a single dot being tracked.
I get what seems like a memory leak, if i look at the process in my taskManager the allocated mem keeps climbing, slowly when tracking a dot, but much quicker when sending empty frames.

if anybody has found a solution to this or even could suggest a solution would be very useful. i think i'm disposing of any thing thats needs to be cleaned up and i'm "free()"ing the frame straight after getting the data from it. any ideas?????
Birch
Posts: 1139
Joined: Thu Jan 30, 2003 5:00 am
Location: Corvallis, Oregon

Re: c# memLeak?

Post by Birch »

If you post the code for your simple application here we can take a look at it and provide some feedback.
timWP01
Posts: 4
Joined: Mon Jul 27, 2009 2:38 pm

Re: c# memLeak?

Post by timWP01 »

here is the c# for the simple project the whole source is available from here:
http://www.filefactory.com/file/a2dgaae ... e_v100.zip
thanks for the quick reply, and please excuse the bad code i'm pretty new to programming.

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using OptiTrack;
using System.Diagnostics;

namespace standAlone_v100
{
    public struct cameraData
    {
        public string camSerial;
        public bool camAttached;
        public bool camTracking;
        public double xPos;
        public double yPos;
    }

    public partial class Form1 : Form
    { 
        #region init optiTrack stuff
        //intialize camera objects (camera collection, camera, frame, tracked object)
        private NPCameraCollection cameraCollection = new NPCameraCollection();
        private NPCamera camera;
        private NPCameraFrame frame;
        private NPObject trackedObject;
        #endregion
        
        #region optitrack cam options
        //camera options enumerations
        private const int NP_OPTION_THRESHOLD = 5;
        private const int NP_OPTION_EXPOSURE = 46;
        private const int NP_OPTION_OBJECT_MASS_MIN = 12;
        private const int NP_OPTION_OBJECT_MASS_MAX = 13;
        private const int NP_OPTION_OBJECT_MASS_IDEAL = 14;
        private const int NP_OPTION_OBJECT_MASS_OUT_OF_RANGE = 15;

        private const int NP_OPTION_FRAME_DECIMATION = 52; 
        private const int NP_OPTION_VIDEO_TYPE = 48;
        private const int NP_OPTION_TEXT_OVERLAY_OPTION = 74;
        private const int NP_OPTION_SEND_EMPTY_FRAMES = 41;
        #endregion

        cameraData camData = new cameraData();
        bool camRunning = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            exposTBLbl.Text = camExposTB.Value.ToString();
            threshTBLbl.Text = camThreshTB.Value.ToString();
        }

        private void checkCamBtn_Click(object sender, EventArgs e)
        {
            try
            {
                camData.camAttached = false;
                camData.camSerial = "none";
                cameraCollection.Enum();
                if (cameraCollection.Count > 0)
                {
                    camera = (NPCameraClass)cameraCollection.Item(0);
                    camData.camAttached = true;
                    camData.camSerial = camera.SerialNumber.ToString();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                cameraConnectedLbl.Text = camData.camAttached.ToString();
                camSerialTxt.Text = camData.camSerial;
            }

        }

        private void camExposTB_Scroll(object sender, EventArgs e)
        {
            exposTBLbl.Text = camExposTB.Value.ToString();
            camera.SetOption(NP_OPTION_EXPOSURE, camExposTB.Value);
        }

        private void camThreshTB_Scroll(object sender, EventArgs e)
        {
            threshTBLbl.Text = camThreshTB.Value.ToString();
            camera.SetOption(NP_OPTION_THRESHOLD, camThreshTB.Value);
        }

        private void camStartStopBtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (!camRunning && camData.camAttached)
                {
                    camera.Open();
                    camera.SetOption(NP_OPTION_SEND_EMPTY_FRAMES, true);
                    camera.SetOption(NP_OPTION_VIDEO_TYPE, 0);
                    camera.SetOption(NP_OPTION_FRAME_DECIMATION, 0);
                    camera.SetOption(NP_OPTION_THRESHOLD, camThreshTB.Value);
                    camera.SetOption(NP_OPTION_EXPOSURE, camExposTB.Value);
                    camera.Start();
                    camRunning = true;
                    camStartStopBtn.Text = "stop camera";
                    timer1.Enabled = true;
                }
                else if (camRunning && camData.camAttached)
                {
                    timer1.Enabled = false;
                    camera.Stop();
                    camera.Close();
                    camRunning = false;
                    camStartStopBtn.Text = "start camera";
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }

        protected void getFrame()
        {
            try
            {
                camData.camTracking = false;
                frame = camera.GetFrame(0);
                if (frame != null)
                {
                    if (!frame.IsCorrupt && !frame.IsEmpty)
                    {
                        foreach (NPObject traking in frame)
                        {
                            if (traking.Rank == 1)
                            {
                                trackedObject = traking;
                                frame.Free();
                            }
                        }
                        camData.xPos = double.Parse(trackedObject.X.ToString());
                        camData.yPos = double.Parse(trackedObject.Y.ToString());
                        camData.camTracking = true;
                        
                    }
                    else
                    {
                        frame.Free();
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (camRunning)
            {
                getFrame();
                xPosTxt.Text = camData.xPos.ToString();
                yPosTxt.Text = camData.yPos.ToString();
                trackingCanTxt.Text = camData.camTracking.ToString();
            }
        }
    }
}
Birch
Posts: 1139
Joined: Thu Jan 30, 2003 5:00 am
Location: Corvallis, Oregon

Re: c# memLeak?

Post by Birch »

First we would recommend only calling Free on your frame at the end of that If block. See if that makes a difference.

Code: Select all

if (frame != null)
{
    if (!frame.IsCorrupt && !frame.IsEmpty)
    {
        foreach (NPObject traking in frame)
        {
            if (traking.Rank == 1)
            {
                trackedObject = traking;
            }
        }
        camData.xPos = double.Parse(trackedObject.X.ToString());
        camData.yPos = double.Parse(trackedObject.Y.ToString());
        camData.camTracking = true;
    }

    frame.Free();
}


Also, I don't know if its necessary in C#, but for C++ we recommend doing the following when releasing a frame :

Code: Select all

m_spFrame->Free();  //free the buffer
m_spFrame = NULL;   //release the COM object.
timWP01
Posts: 4
Joined: Mon Jul 27, 2009 2:38 pm

Re: c# memLeak?

Post by timWP01 »

Thanks for your help, seems to be better if i enable sending empty frames....? but after extensive testing (of my final App), seems as if the program fluctuates in memory use by 30MB. Increasing in small increments then having large drops. While totally fine for my usage, is this common?
Birch
Posts: 1139
Joined: Thu Jan 30, 2003 5:00 am
Location: Corvallis, Oregon

Re: c# memLeak?

Post by Birch »

Its not uncommon to see some fluctuation in memory usage. What you are seeing in your application might be related to C# doing background garbage collection.
Post Reply