Hello. I have a picture box on a form. The imageLocation of the picture box is set to a url like for example "http://www.google.mu/imgres?imgurl=http://1.bp.blogspot.com/-w6AJJ5Xoulg/T3AymMKbFlI/AAAAAAAAACI/EslCtw42HHg/s1600/jpeg.jpg&imgrefurl=http://zainriasat786.blogspot.com/2012/03/discuss-impact-that-file-format.html&h=480&w=640&sz=36&tbnid=fkqhzaWg8cSK4M:&tbnh=90&tbnw=120&zoom=1&usg=__54xTPooPnJOFy8xQS0TJXOtV1d8=&docid=JkpXnRk64SCIzM&hl=en&sa=X&ei=LjxoUO_uEsXRrQf28oDYBw&sqi=2&ved=0CD0Q9QEwBA&dur=24". When the form closes, we can see that memory is not released. I have added code to dispose the picture box on form closing but when I open the form again, the form crashes.
Is .Dispose() the best way to prevent memory leaks?
Jump to Post
Memory isn't release immediately (though it may be). The garbage collector runs when it thinks it should.
But yes, using Dispose is what you do on objects that maintain non-managed resources. If your form crashes when you try to open it again, that's a coding issue on your part :)
Jump to Post
Just so I'm clear, you are instantiating a new instance of your Form (i.e.
new SomethingForm();) before each call to
ShowDialog();, correct? I tried your code, and it seems to be working. Also, a stack trace is nice, but what is the actual exception you are getting (class and message)?
All 8 Replies
We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.