I know... I know... yet another silly question about pointer management. Sorry about that.
But I've been stuck with this for a day now. I am using a copy of the code used by Jules in the Juce Demo to play audio files:
void loadFileIntoTransport (const File& audioFile)
{
// unload the previous file source and delete it..
transportSource.stop();
transportSource.setSource (nullptr);
currentAudioFileSource = nullptr;
AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
if (reader != nullptr)
{
currentAudioFileSource = new AudioFormatReaderSource (reader, true);
// ..and plug it into our transport source
transportSource.setSource (currentAudioFileSource,
32768, // tells it to buffer this many samples ahead
&thread, // this is the background thread to use for reading-ahead
reader->sampleRate); // allows for sample rate correction
}
}
It works fine in my code but I get Leaks when I exit the app if I call this routine more than once.
I could understand that the reader pointed to by "reader" would not be deleted after the first call (if I call it 4 times I will get 4 leaks), but why does it work in Jules code and not in mine ??? I tried many (bad) things, but could not get rid of the problem. Any hint please ?
If you've had to resort to raw pointers you still have a problem somewhere! You're probably either deleting an object owned by the AudioFormatReaderSource by keeping a ScopedPointer to it in your class or failing to delete an object somewhere. This is why I asked to see the error message, there are two that come from the JUCE_LEAK_DETECTOR, one a double deletion, one a leak.
Also try and keep objects scoped as narrowly as possible, preferring to declare them on the stack. This avoids you having to manage their lifetimes.
The fact that loadFileIntoTransport takes a pointer that is owned by something else sets off a few alarm bells. Why are you passing something called fileToRead into a MemoryInputStream? It's impossible to tell from the snippets what the problem is, it doesn't show the whole picture.
Thank you for the hints. I will re-check when I have a little time (not now...). I will let you (and others) know what was wrong, if anything. Right now what I have works but it is more painful because I have to take care of some pointed objects lifetimes...
Regarding fileToRead etc. it is just a bad copy/paste as I use the same scheme to read from external files.
In this particular case I read the data from BinaryData, fileToRead being the pointer and fileSize the size.