I had to change the way my copy paste manager worked for Juce 7 as the prior version didn’t anymore. At the same time I consolidated my .cpp and .h files into a .h file and I might have butchered the Singleton code.
I am now getting memory leaks upon closing the running program and I’m not sure which of these two changes are triggering it.
The copy/paste manager still works but I obviously don’t want memory leaks. Any ideas? Did I do the Singleton okay? Is the unique_ptr being used wrong? Thanks for any help.
#pragma once
#include "JuceHeader.h"
class CopyPasteManager
{
public:
CopyPasteManager() {};
~CopyPasteManager() {
DBG("DESTRUCTOR RUN");
mCopyState.reset();
};
void copy(AudioProcessorValueTreeState* inState) {
juce::ValueTree state = inState->copyState();
std::unique_ptr<XmlElement> xmlStateCopy = state.createXml(); //is this a memory leak???
mCopyState.reset(xmlStateCopy.release());
}
void paste(AudioProcessorValueTreeState* inState) {
if (mCopyState->hasTagName(inState->state.getType())) {
inState->replaceState(ValueTree::fromXml(*mCopyState));
}
}
juce_DeclareSingleton (CopyPasteManager, false)
private:
std::unique_ptr<XmlElement> mCopyState;
};
juce_ImplementSingleton(CopyPasteManager)
I presume I am not handling the unique_ptr
in copy
properly but have spent an hour trying different things and can’t help that using this function provokes
*** Leaked objects detected: 231 instance(s) of class XmlElement
JUCE Assertion failure in juce_LeakedObjectDetector.h:92
A breakpoint instruction (__debugbreak() statement or a similar call) was executed in SinSynth.exe.
Prior copy
code which worked without leaks was as follows, but this is somehow not working in JUCE 7 because state.createXml() now makes another unique_ptr
rather than the xml itself:
auto state = inState->copyState();
mCopyState.reset(state.createXml());
I appreciate any suggestion to fix it. Thanks again.