I\'m trying to take a snapshot of the whole screen for reading pixel values. Actually i\'m doing it without any problem. But after exactly 214 snapshots, i\'m getting out of mem
You need to dispose disposable resources once you're done working with them. Bitmap
class implements IDisposable
- so it is disposable resource. Correct pattern is instead of
Bitmap bmp = TakeSnapshot();
var c = bmp.GetPixel(0,0);
Something like
Bitmap bmp = null;
try
{
bmp = TakeSnapshot();
var c = bmp.GetPixel(0,0);
// any more work with bmp
}
finally
{
if (bmp != null)
{
bmp.Dipose();
}
}
Or in short form (which is preferable):
using(Bitmap bmp = TakeSnapshot())
{
var c = bmp.GetPixel(0,0);
// any more work with bmp
}
Reference: Using Objects That Implement IDisposable
Edit
You can easily emulate the issue:
public class TestDispose : IDisposable
{
private IntPtr m_Chunk;
private int m_Counter;
private static int s_Counter;
public TestDispose()
{
m_Counter = s_Counter++;
// get 256 MB
m_Chunk = Marshal.AllocHGlobal(1024 * 1024 * 256);
Debug.WriteLine("TestDispose {0} constructor called.", m_Counter);
}
public void Dispose()
{
Debug.WriteLine("TestDispose {0} dispose called.", m_Counter);
Marshal.FreeHGlobal(m_Chunk);
m_Chunk = IntPtr.Zero;
}
}
class Program
{
static void Main(string[] args)
{
for(var i = 0; i < 1000; i ++)
{
var foo = new TestDispose();
}
Console.WriteLine("Press any key to end...");
Console.In.ReadLine();
}
}