2

I'm currently programming a Program which displays all pictures of a folder after another, but although I use dispose before I load the new Image, I get the OutOfMemory exception after a few picture. My Task Manager shows nearly constant RAM usage so there shouldn't be a Memory Leak...

private void loadImage()
{
    if (pictureBox1.Image != null)
    {
        pictureBox1.Image.Dispose();
    }
    pictureBox1.Image = Image.FromFile(filenames[index]);
}
TaW
  • 53,122
  • 8
  • 69
  • 111
Soziapath
  • 21
  • 3
  • 1
    Your code to dispose looks fine. Pretty sure it's the last line causing the issue when it encounters something it can't load. See the accepted answer here for more information: https://stackoverflow.com/questions/2449587/out-of-memory-with-multi-images-in-one-picturebox – Equalsk Jul 18 '15 at 20:58
  • Always the same file that throws? How large are the files? – TaW Jul 18 '15 at 21:43
  • If you check your taskmanager on handles. will it increase? I think about file handles getting lost. – Jeroen van Langen Jul 18 '15 at 21:51
  • Do you call load images one by one within a timer tick or in a click event handler or do you load them all together from the same event ? – Graffito Jul 18 '15 at 22:00
  • I tried the exact same thing in Visual Studio 2013 instead of VS 2012 and there it works, but in VS2012 i always get this exception. Maybe a bug in my .net version? @Graffito i load them within a click event handler. – Soziapath Jul 19 '15 at 00:57
  • Most certainly not a bug in .Net. Any reason why you didn't answer our questions? What are the numbers and the sizes of the files involved? – TaW Jul 19 '15 at 07:14

0 Answers0