CameraDevice::openDevice() static function leaking memory


#1

Hi there!!

I am new to JUCE, however I have been strugling to discover why CameraDevice::openDevice() static function leaks memory even if you delete the CameraDevice returned.

I mean I was leaking memory in my own project but then I openned and debugged the cameraDemo, and I realized that even the demmmo drains memory each time you open a device, it gets destroyed but the without releasing the same amount of memory it took.

Anyone knows why this happenned or how to fix it? I have just kept the pointers to CameraDevices it returns instead of deleting them because of this, however I think it can be a bug to be fixed in the future.

Thx in advance!!


#2

Hi @mmartinm!

juce::CameraDevice is a cross-platform abstraction, which has very different implementations on each platform. In order to help you track down this memory leak, we need to know on which platform you are running and debugging your own project and the CameraDemo.


#3

We also need to know how you are detecting your leaks.

We get a fair number of people reporting that the system memory usage is increasing with runtime, but this often just means that the OS is choosing not to reuse memory for new allocations, rather than an actual memory leak. If, however, you are triggering JUCE’s leak detector, you can show that a destructor isn’t being called for a matching constructor, or you are seeing un-freed memory messages from your IDE, compiler or Valgrind then that’s worth investigating.


#4

Hi @McMartin and @t0m. First of ll thank you or the quick answer to my issue.
I am running it in Windows 10 and with visual studio 2017. I have detected the leak looking at the memory used by application with the debugging tools of visual studio and also with the Windows task manager both in release and debug.

I am not getting any juce leak detection for not destroying appropriately the object. If you just run the camera demo and look at its stats in the task manager you can realise that switching from no camera to the local webcam or other cam like the usb capture device that I am using the memory increases every time when it is supposed to stay stable because the camera demo creates and destroys the objected previously opened.

So as you say is maybe de SO I don’t know but it doesn’t seem normal to me.

Thank you both and I hope we can track this bug.


#5

As I said, Windows can choose not to reuse memory it has freed. This means that indicators like the Windows task manager and similar often make it look like your application is using increasing amounts of memory with time, but the reality is that everything is being cleaned up correctly.

If you can demonstrate a leak with a compiler tool, such as https://docs.microsoft.com/en-us/visualstudio/profiling/memory-usage?view=vs-2017, then this would indicate an actual bug and we’ll investigate.


#6

Yes exactly, that’s how I realized for the first time. I attached a screen image showing that each time the camera demo destroys a device and opens a new one, the memory used increases more than it frees without upper limit!

I whink it’s worth to investigate.

Summary


#7

Sorry, perhaps I’m not being clear enough.

Any indicator that is just a simple measure of how much memory a process is using, including the graph in Visual Studio’s Diagnostic Tools window, can display increasing memory usage because the OS decides not to reuse memory that has actually been freed by the process.

I sent you that link because it describes the process of taking a snapshot of the memory used by your program, which you can then compare with a reference state. This is the only reliable way to detect leaks in your code, other than actively monitoring calls to destructors or other purpose built compiler tools like Valgrind.

If you do this after going through the process of opening then closing the camera you will see that the size of the heap stays more or less constant (it will fluctuate a little because it contains things like GUI event-loop messages), but the graph shows a 6 MB jump upwards.