2

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.

grandcameo
  • 185
  • 2
  • 11

2 Answers2

4

Several things:

  1. You're mixing wide strings and Microsoft ("T") strings. Pick one (whichever fits your API).

  2. 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.

dplante
  • 2,445
  • 3
  • 21
  • 27
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Just looked at the documentation, maybe you can even say `FileInputStream fileInputStream(fileHard);` and `midFile.readFrom(fileInputStream);` to get rid of that ugly pointer and the associated clean-up responsibilities. – Kerrek SB Sep 13 '11 at 00:43
  • Even better! Thank you so much! – grandcameo Sep 13 '11 at 02:56
  • 1
    "2. Don't say new in C++"??.. new is what C++ is all about, the automatic objects are doing it too behind the scenes. I wouldn't say that, you will just have to know what you´re doing. – Magnus Sep 24 '13 at 13:34
0

Some Message object leaks are unavoidable when building VST Plugins, this is a problem with the Steinberg interface and not your problem. For more information, visit the Juce forum at http://www.rawmaterialsoftware.com/index.php

Vinnie Falco
  • 5,173
  • 28
  • 43