I am a PHP programmer learning C++ as I build a VST host. I may have bitten off more than I can chew but I am making some progress (I think)!
I'm using the Steinberg VST SDK and the JUCE library in Visual Studio 2010. I'm encountering a leaked object error and I don't quite understand the solutions I've found when I've searched for the error I received.
Here's the error in the Output tab. My program spits out JUCE Assetion error:
*** Leaked objects detected: 44 instance(s) of class MidiEventHolder
score.exe has triggered a breakpoint
I am taken to this message in the juce_amalgamated.h file:
~LeakCounter()
{
if (numObjects.value > 0)
{
DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());
/** If you hit this, then you've leaked one or more objects of the type specified by
the 'OwnerClass' template parameter - the name should have been printed by the line above.
If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
*/
jassertfalse;
}
}
Here is the bit I code I believe the error is referring to:
const wchar_t* midiPath = L"C:\\creative\\midi\\m1.mid";
File* fileHard;
FileInputStream* fileInputStream;
fileHard = new File (T("C:\\creative\\midi\\m1.mid"));
fileInputStream = fileHard->createInputStream();
MidiFile * midFile;
midFile = new MidiFile();
midFile->readFrom(*fileInputStream);
midFile->getNumTracks();
midFile->getTrack(0);
Maybe I'm approaching this syntax more like it's PHP? I didn't quite understand what RAII techniques were.
Any tips to get me in the right direction are appreciated.
Several things:
You're mixing wide strings and Microsoft ("T
") strings. Pick one (whichever fits your API).
Don't say new
in C++. It's almost always not what you need. Instead, just use automatic objects:
File fileHard("C:\\creative\\midi\\m1.mid");
FileInputStream * fileInputStream = fileHard.createInputStream();
MidiFile midFile;
midFile.readFrom(*fileInputStream);
midFile.getNumTracks();
midFile.getTrack(0);
That should get rid of most your memory leaks. You will still need to free up the fileInputStream
in a manner described in your documentation.