C
C#10mo ago
!Rushaan

Out of memory error

Using AForge.Video.DirectShow to integrate a QR Code scanner in my WPF App but whenever the scanner is open it crashes in 15 seconds due to out of memory error as it reaches 4gb memory usage and I'm not able to exactly pinpoint the reason why its happening, BitmapToImageSource() throws the out of memory error. https://pastebin.com/XjsE9BuJ
10 Replies
Lecco
Lecco10mo ago
I think if you use Dispose() function,can solve memory error problem. try it!
!Rushaan
!RushaanOP10mo ago
i tried bitmap.Dispose() and it didn't make a difference
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();

Application.Current.Dispatcher.Invoke(() =>
{
cameraImage.Source = BitmapToImageSource(bitmap);
});

BarcodeReader barcodeReader = new BarcodeReader();
Result result = barcodeReader.Decode(bitmap);

if (result != null)
{
string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string initialDirectory = System.IO.Path.Combine(downloadsPath, "QR Codes");

if (!Directory.Exists(initialDirectory))
{
Directory.CreateDirectory(initialDirectory);
}

DateTime currentDateTime = DateTime.Now;
string fdt = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss");
string fileName = "QRCode " + fdt + ".png";

fileName = CleanFileName(fileName);

string path = System.IO.Path.Combine(initialDirectory, fileName);
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png);
videoSource.NewFrame -= VideoSource_NewFrame;
videoSource.SignalToStop();
bitmap.Dispose();
System.Threading.Tasks.Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(() =>
{
cameraImage.Source = null;
videoSource = null;
videoDevices = null;
myframe.frame.Content = new Scanned(bitmap, path, false, false);
});
});
}
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();

Application.Current.Dispatcher.Invoke(() =>
{
cameraImage.Source = BitmapToImageSource(bitmap);
});

BarcodeReader barcodeReader = new BarcodeReader();
Result result = barcodeReader.Decode(bitmap);

if (result != null)
{
string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string initialDirectory = System.IO.Path.Combine(downloadsPath, "QR Codes");

if (!Directory.Exists(initialDirectory))
{
Directory.CreateDirectory(initialDirectory);
}

DateTime currentDateTime = DateTime.Now;
string fdt = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss");
string fileName = "QRCode " + fdt + ".png";

fileName = CleanFileName(fileName);

string path = System.IO.Path.Combine(initialDirectory, fileName);
bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png);
videoSource.NewFrame -= VideoSource_NewFrame;
videoSource.SignalToStop();
bitmap.Dispose();
System.Threading.Tasks.Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(() =>
{
cameraImage.Source = null;
videoSource = null;
videoDevices = null;
myframe.frame.Content = new Scanned(bitmap, path, false, false);
});
});
}
Lecco
Lecco10mo ago
sorry. it is my mistake
!Rushaan
!RushaanOP10mo ago
Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'NewFrameEventArgs.Frame' C:\Users\rusha\source\repos\QRCodeApp\QRCodeApp\Scanner.xaml.cs 46
Lecco
Lecco10mo ago
Can you share your project? or use Documentation.
!Rushaan
!RushaanOP10mo ago
https://pastebin.com/XjsE9BuJ that's the entire scanner page
Lecco
Lecco10mo ago
another possibility is use "using": code : private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs) { try { using (Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone()) { Application.Current.Dispatcher.Invoke(() => { cameraImage.Source = BitmapToImageSource(bitmap); });
BarcodeReader barcodeReader = new BarcodeReader(); Result result = barcodeReader.Decode(bitmap);
if (result != null) { string downloadsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string initialDirectory = Path.Combine(downloadsPath, "QR Codes");
if (!Directory.Exists(initialDirectory)) { Directory.CreateDirectory(initialDirectory); }
DateTime currentDateTime = DateTime.Now; string fdt = currentDateTime.ToString("yyyy-MM-dd HH_mm_ss"); string fileName = "QRCode " + fdt + ".png";
fileName = CleanFileName(fileName);
string path = Path.Combine(initialDirectory, fileName); bitmap.Save(path, System.Drawing.Imaging.ImageFormat.Png); videoSource.NewFrame -= VideoSource_NewFrame; videoSource.SignalToStop();
System.Threading.Tasks.Task.Run(() => { Application.Current.Dispatcher.Invoke(() => { cameraImage.Source = null; videoSource = null; videoDevices = null; myframe.frame.Content = new Scanned(bitmap, path, false, false); }); }); } } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "An Exception occured"); } } try this code. How is about? How is about?
!Rushaan
!RushaanOP10mo ago
im trying it rn It didn't fix it and the memory usage still increases at the same rate and gives out of memory exception https://stackoverflow.com/questions/78074037/wpf-out-of-memory-error-in-a-qr-code-scanner
Keswiik
Keswiik10mo ago
You never dispose of the bitmap you create in BitmapToImageSource that you are assigning to cameraImage.Source
!Rushaan
!RushaanOP10mo ago
yup all that time I wasn't able to figure out how to exactly dispose the bitmap and which one but now i entirely fixed it: https://stackoverflow.com/a/78082665/23190731
Want results from more Discord servers?
Add your server