Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:
C#
string cmdCommand = "/C py e:/scripts/python/pattern_gen/chalk_stripes.py";
			Process process = new Process();
			process.StartInfo.RedirectStandardOutput = true;
			process.StartInfo.UseShellExecute = false;
			process.StartInfo.CreateNoWindow = true;
			process.StartInfo.FileName = "CMD.exe";
			process.StartInfo.Arguments = cmdCommand;
			process.Start();
			while (true)
			{
				if (File.Exists(path))
				{
					process.Close();
					process.Dispose();
					try
					{
						Image image = Image.FromFile(path);
						pictureBox1.Image = new Bitmap(image);
						image.Dispose();
						break;
					}
					catch
					{
						MessageBox.Show("ERROR: OUT OF MEMORY");
						this.Close();
					}
				}
			}

Basically I have a python script that generates a random chalk stripe pattern and saves it to the same path that I have set the variable "path" to. It works sometimes but maybe every 2/3 times gives the error message I made to fix the entire application crashing. How do I fix this? Tell me if you need more information, thanks!

What I have tried:

I have tried changing the file format it is changed in just incase it was an inefficient file format as the dimension of the pattern generated and picture box is just 400x400 but I have made a similar script using python to generate another pattern with 500x1000 resolution. The 500x1000 was in .png and I tried .jpg with this, switched it over to .png and it still gives me out of memory.
Posted
Updated 7-Jan-22 9:25am

Why are you assuming that it's an "out of memory" error? You don't even look at at the exception itself to find out what the system is complaining about.

Don't ignore exception codes:
C#
catch (Exception ex)
   {
   MessageBox.Show(ex.Message);
   Close();
   }
I think you will find the exception is more like "attempt to use an object after it has been Disposed" than out of memory ...
 
Share this answer
 
Lot's of problems. Well, first, your catch block is swallowing the actual exception. DO NOT DO THAT! You're hiding what the code is complaining about. Report the actual exception:
C#
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    Close();
}

Next, when you launch your process you're not waiting for it to complete properly. All you're doing is looping, waiting for a filename to show up, but that does NOT mean the file is a valid image file when the filename shows up. The process you launched may not have completed writing the file before you try to read it. That's probably where the exception is coming from.

Call .WaitForExit() on the process you launched instead of using a bad looping method to look for a result file.
C#
...
process.StartInfo.FileName = "CMD.exe";
process.StartInfo.Arguments = cmdCommand;
process.Start();
process.WaitForExit();
process.Dispose();

Image image = Image.FromFile(path);
pictureBox1.Image = new Bitmap(image);

In your code, you're calling image.Dispose() right after you assign the image to the PictureBox. YOU CANNOT DO THAT! That destroys the image object and even the PictureBox cannot display it after that.

The technique you're using to create the image object is also flawed. It will hold the image file locked open for the entire life of the image object. The fix is easy, open the image file as a stream, read it, then close the file:
C#
Bitmap image;
using (FileStream stream = new FileStream(path), FileMode.Open, FileAccess.Read)
{
    image = Image.FromStream(stream);
    picturebox1.Image = image;
}
 
Share this answer
 
v2
Comments
OriginalGriff 7-Jan-22 15:57pm    
"In your code, you're calling image.Dispose() right after you assign the image to the PictureBox. YOU CANNOT DO THAT! That destroys the image object and even the PictureBox cannot display it after that."
Look again - he uses the image he read from the file to create a new Bitmap which the picture box displays - the original Image can be disposed as it isn't needed any more.
Dave Kreskowiak 7-Jan-22 16:08pm    
Oh, crap. That's right. I didn't see that. Even thinking about that, it makes my skin crawl creating two Image objects just to have one of them die immediately.
OriginalGriff 7-Jan-22 16:16pm    
It was an odd decision on MS's part, that's for sure ... :D
Dave Kreskowiak 7-Jan-22 16:18pm    
Yeah, no kidding. Why would you EVER want to hold the image file open? It should have been an opt-in option if you wanted the file locked for the life of the image object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900