I am getting a lot of leaked errors

I created an audio DJ that also uses tables to add songs to it etc but when I close it I get a lot of leaked errors.

*** Leaked objects detected: 13 instance(s) of class JuceIStream
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class WMAudioReader
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class DynamicLibrary
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class AudioFormatReader
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class FileInputStream
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class MemoryBlock
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class InputStream
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 13 instance(s) of class StringPairArray
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
*** Leaked objects detected: 26 instance(s) of class StringArray
JUCE Assertion failure in juce_LeakedObjectDetector.h:92

You are probably using raw pointers or global/static variables for your objects. Then you will easily get leaks of multiple kinds of Juce objects. It’s impossible to say without seeing the whole code why exactly you are getting the leaks.

What are raw pointers? I thought pointers were just pointers, I do use global variables for some objects.

Pointers are just pointers, but they require that you manually manage their lifetimes. Ie. You have to remember to delete the allocated memory when you are done with it.

BUT, the good news is you can use some cool objects from the standard library to help automate that. The most common is ‘std::unique_pointer’. This holds your raw pointer, and when the ‘unique _ptr’ object goes out of scope, it automatically deletes the allocated memory. In modern c++ you should default to using a ‘unique_ptr’, consider other pointer containers for some specific use cases, and only use raw pointers when required (for reasons, :wink:)

1 Like

You shouldn’t use regular global variables for Juce objects, as the leak detection mechanism in Juce doesn’t really work with those. There’s a way to make global variables that is compatible with the Juce leak detection, but you should consider not using the globals to begin with. There’s rarely any good reason to use global variables. (That those can make some things easier for the programmer is not a good reason.)

auto* reader = formatManager.createReaderFor(juce::URL{ file }.createInputStream(false));
		if (reader != nullptr)
		{
			auto lengthInMinutes = (reader->lengthInSamples / reader->sampleRate) / 60;

			//Get the length by two decimal places
			std::stringstream stream;
			stream << std::fixed << std::setprecision(2) << lengthInMinutes;
			std::string lengthOfTrack = stream.str();

			trackLength.push_back(lengthOfTrack);
		}

		saveToFile();
		tableComponent.updateContent();
	}
	else
	{
		DBG("PlaylistComponent::addTrackToTable<< Do not enter a duplicate Track!");
	}

This bit of code surely leaks, I’m not sure why of course, it’s a way for me to get the length of a song to display.

Global variables meaning it’s global to every function in that class right? I’ll have to find a way to cut down on most of them but I do need global variables for most

Variables in classes are not “global”, they are “member variables”. Unless they are “static” members. Global variable is something declared outside a class context, and which could be accessed all over the code.

My JUCE project has no global variables then, it’s literally just classes only that links to the MainComponent class

You are not deleting the reader returned by the format manager in that code, so you get a leak.

At the end of the same if statement I tried setting reader to null but the same thing happened

The reader returned by the manager is a raw pointer. Nulling those is not going to actually delete the object.

I see, I ended up turning it into a smart unique pointer and it worked, thanks a lot guys!

1 Like