AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

C# System.OutOfMemoryException: 'Out of memory.'

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

C# System.OutOfMemoryException: 'Out of memory.'

Postby btb4198 » Thu Jan 03, 2019 11:20 pm

I am using videoSourcePlayer from AForge. now I had to add a function to it because GetCurrentVideoFrame( ) was not working the why I needed. So I make a function called GetCurrent() and it work the way I wanted. The problem I am having is when I used it in place of GetCurrentVideoFrame( ) I get a System.OutOfMemoryException: 'Out of memory.' Exception and I have no Ideal why. here is my code :

Code: Select all
Bitmap getPic2(int i2)
    {
        Bitmap bmp = null;
        Bitmap tempB = null;
        if (endIRList[i2].X > videoSourcePlayer.Width - 1)
            endIRList[i2]= new System.Drawing.Point(videoSourcePlayer.Width - 1, endIRList[i2].Y);
        if (endIRList[i2].Y > videoSourcePlayer.Height - 1)
            endIRList[i2] = new System.Drawing.Point(endIRList[i2].X, videoSourcePlayer.Height - 1);
        if (stIRList[i2].X >= 0 && stIRList[i2].Y >= 0 && endIRList[i2].X < videoSourcePlayer.Width && endIRList[i2].Y < videoSourcePlayer.Height)
        {
            if (endIRList[i2].X - stIRList[i2].X > 0 && endIRList[i2].Y - stIRList[i2].Y > 0)
            {
                bmp = videoSourcePlayer.GetCurrent();
                System.Drawing.Image iOld = p2.Image;
                tempB = bmp.Clone(new Rectangle(stIRList[i2].X, stIRList[i2].Y, endIRList[i2].X - stIRList[i2].X, endIRList[i2].Y - stIRList[i2].Y),bmp.PixelFormat);

                if (iOld != null)
                {
                    iOld.Dispose();
                    iOld = null;
                }
            }
        }
        pictureBox1.Image =this.videoSourcePlayer.GetCurrent();

        TestPicBox.Image = tempB;


        return tempB;
    }

the problem I am having is at:
tempB = bmp.Clone(new Rectangle(stIRList[i2].X, stIRList[i2].Y, endIRList[i2].X - stIRList[i2].X, endIRList[i2].Y - stIRList[i2].Y),bmp.PixelFormat);

now if I just use bmp = GetCurrentVideoFrame I do not get the problem. so something most be wrong with my function GetCurrentVideo

here is the code :

Code: Select all
public Bitmap GetCurrentVideoFrame( )
    {
        lock ( sync )
        {
            return ( currentFrame == null ) ? null : AForge.Imaging.Image.Clone( currentFrame );
        }
    }

    public Bitmap GetCurrent()
    {
        lock (sync)
        {

            Bitmap original = GetCurrentVideoFrame();
            currentPic = new Bitmap(original, new Size(original.Width / 2, original.Height / 2));
            original.Dispose();
            original = null;
            return currentPic;
        }

    }


I just cant see why their function works and my does not. can anyone help?
btb4198
 
Posts: 31
Joined: Fri May 18, 2018 2:31 am

Re: C# System.OutOfMemoryException: 'Out of memory.'

Postby andrew.kirillov » Fri Jan 04, 2019 7:13 am

Most probably you don't dispose all resources. I see you are cloning image - do you dispose them after that? Also you are setting images to picture boxes, but can not see you disposing the old ones. Simply clean up in your code and it should get better with memory.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3446
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: C# System.OutOfMemoryException: 'Out of memory.'

Postby btb4198 » Fri Jan 04, 2019 1:10 pm

I dispose original
i cant not dispose currentPic because i am returning it.
and it dies before on tempB = bmp.Clone(new Rectangle(stIRList[i2].X, stIRList[i2].Y, endIRList[i2].X - stIRList[i2].X, endIRList[i2].Y - stIRList[i2].Y),bmp.PixelFormat);

also this code words for your function
GetCurrentVideoFrame

why does it not work for mines ?
what am I doing wrong ?
btb4198
 
Posts: 31
Joined: Fri May 18, 2018 2:31 am

Re: C# System.OutOfMemoryException: 'Out of memory.'

Postby btb4198 » Sat Jan 05, 2019 1:52 pm

can anyone help ?
I really need to get this to work
btb4198
 
Posts: 31
Joined: Fri May 18, 2018 2:31 am




Return to AForge.NET Framework