Newbie Needs Help w./ Regards Memory Leak


#1

Hey guys!

 

As the title implies I'm a JUCE newbie. This is my first time using it, and I'm only using the audio-basics library so I can read in MIDI files and convert them into a beat map for a music game I'm developing as part of a senior project in college.

 

When I exit my application right now, I hit a breakpoint and am informed of the following via VS2012's output window:


*** Leaked objects detected: 69 instance(s) of class MidiEventHolder
JUCE Assertion failure in juce_leakedobjectdetector.h:95

The code where these 69 instances are:


std::vector<NoteGem*> MIDIManager::parseTrack(const char* filename)
{
    juce::MidiFile* midi_file = new juce::MidiFile();
    juce::File midi_source_file = juce::File::getCurrentWorkingDirectory().getChildFile(filename);
    juce::FileInputStream* midi_source = new juce::FileInputStream(midi_source_file);

    if (midi_file->readFrom(*midi_source))
    {
        const juce::MidiMessageSequence* note_track = midi_file->getTrack(0);
        std::vector<juce::MidiMessageSequence::MidiEventHolder*> midi_events;
        int num_events = note_track->getNumEvents();
        std::cout << "Number of Events:: " << num_events << '\n';
        for (int i = 0; i < num_events; i++)
        {
            midi_events.push_back(note_track->getEventPointer(i));
        }
        std::vector<NoteGem*> note_gems;
        for (int j = 0; j < num_events; j++)
        {
            std::cout << "MIDIManager::ParseTrack() Note Info:\n"
                << "Note On?:: " << midi_events[j]->message.isNoteOn() << '\n'
                << "Timestamp:: " << midi_events[j]->message.getTimeStamp() << '\n' 
                << "Note Number:: " << midi_events[j]->message.getNoteNumber() << '\n';
            if (midi_events[j]->message.isNoteOn())
            {
                NoteGem* note;
                switch(midi_events[j]->message.getNoteNumber())
                {
                    case 72:
                        note = new Tap(A, midi_events[j]->message.getTimeStamp());
                        note_gems.push_back(note);
                        break;
                    case 73:
                        note = new Tap(B, midi_events[j]->message.getTimeStamp());
                        note_gems.push_back(note);
                        break;
                    case 74:
                        note = new Tap(X, midi_events[j]->message.getTimeStamp());
                        note_gems.push_back(note);
                        break;
                };
            }
        }
        return note_gems;
    }
    else
    {
        std::cout << "MIDIManager::ParseTrack() Error: MIDI File not read!\n";
        std::vector<NoteGem*> empty_events;
        return empty_events;
    }
}

I'm guessing there's some sort of clean up after I've gotten the information I need. But I'm not sure what that would entail in this case. Any help would be appreciated, and apologies if this is a stupid question.

 

Thanks!

 

 


#2

do you need to delete the MidiFile object allocated at the start?

 

 


#3

There's a page on the juce site here that has lots of good tips for coding with juce.  It's worth checking out if you're new to juce or even just looking for some good c++ advice.  

Try to only "new" things when you absolutely must, and if you must then use the provided helpers for managing memory on the heap (i.e. anything allocated using "new") such as ScopedPointer, ReferenceCountedObjectPtr, OwnedArray, ReferenceCountedObjectArray...etc.