Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# Garbage collector problem

Options
  • 07-02-2005 6:08pm
    #1
    Registered Users Posts: 2,758 ✭✭✭


    Hi there,

    I'm having a memory leak problem and after a bit of debugging i've found the problem, except imo it shouldn't be a problem.

    I have the following code:
    private void WebCamCapture_ImageCaptured(object source, SerialCommApp.WebcamEventArgs e)
    {
    	if(capture == true)
    	{
    		capture = false;
    		Graphics graphicImage = Graphics.FromImage(e.WebCamImage ); 
    		
    
    		graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
    		graphicImage.FillRectangle(SystemBrushes.InactiveCaption , 10, 220  , 250 , 50);
    		
    
    		graphicImage.DrawString("Some string at the bottom of the image", new Font("Arial", 20 ,FontStyle.Bold ), SystemBrushes.HighlightText, new Point( 10, 220 ) ); 
    		pictureBox1.Image = e.WebCamImage;
    		e.WebCamImage.Save("c:\\abc.jpg" , System.Drawing.Imaging.ImageFormat.Jpeg);
    		Application.DoEvents();
    	}	
    
    }
    

    All the above does is grab an image object fromt he capture driver that a web cam is using. Then tags some text onto the bottom of the image and then saves the image to my hdd. The problem is that there's a memory leak.

    If i comment out the code that says "if(capture == true)" then the memory allocation keeps climbing but gets sorted when it gets to about 80,000k, ie. back down to about 20,000k. Its the garbage collector i'm assuming. If the line is there ("if(capture == true)" ) the memory allocation keeps climbing to about 134000-160000 but by that time the system is unusable... windows starts rabbiting on about virtual memory and i might as well go make coffe in the time it takes to stop the project from running.

    i know some people are going to jump on the fact i'm not cleaning up any objects but i don't need to. Its running fine if i take out the if(capture ==true)..

    Any help here would be appreciated.


Advertisement