C
C#4mo 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
Lecco4mo ago
I think if you use Dispose() function,can solve memory error problem. try it!
Rushaan.
Rushaan.4mo 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
Lecco4mo ago
sorry. it is my mistake
Rushaan.
Rushaan.4mo 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
Lecco4mo ago
Can you share your project? or use Documentation.
Rushaan.
Rushaan.4mo ago
https://pastebin.com/XjsE9BuJ that's the entire scanner page
Lecco
Lecco4mo 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.
Rushaan.4mo 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
Keswiik4mo ago
You never dispose of the bitmap you create in BitmapToImageSource that you are assigning to cameraImage.Source
Rushaan.
Rushaan.4mo 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
More Posts
[ASP.NET] Docker build not working properly.so basically i made and pushed a docker image to the hub repositoory. And everytime i push to github[Avalonia] Beginner attempting a spreadsheet application. Having issues displaying rows.I'm really hoping that someone with some experience may have some time to assist me. This is my firsSome people can sent me document c# with all things need itdocumentation .net and asp please and courses to learn more[AVALONIA] Polylines not displaying on CanvasI have been having this issue for a while. My Polylines which I have generated are not being displayCompression with real time compressed size estimationHi all. Is there a something I can use to compress a stream of data while getting a good estimate ofHow to import objects from external, uncompiled .cs files?Let's say I have a file named "myCustomObject.cs", and it has a class definition "customObject1". WVisual Studio stuck on "Working on it" when trying to load a Project's propertiesPretty much as per the title. Right click on a project in the Solution view, choose "Properties" - i[EF Core/LINQ] Paginated Eager-Loaded One-to-ManyI can't seem to find (maybe my Googling is bad) the proper way - if it's possible - to limit the retNeed help with searching an entity and displaying the results using a DataGridView.Hello, I am a student currently stuck on this. The idea is to search using the entity's attributes aPacking an external DLL with my own DLLI've written a library that also uses another external library, packed into a .dll file. When I buil