# AudioFormatReader leaks

**URL:** <https://forum.juce.com/t/audioformatreader-leaks/13752>\
**Category:** General JUCE discussion\
**Created:** [October 21, 2014, 4:23pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752 "2014-10-21T16:23:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [October 21, 2014, 4:23pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/1 "2014-10-21T16:23:06Z")

</div>

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.

The line that generates the leaks is&nbsp;

```
AudioFormatReader* reader = formatManager.createReaderFor (audioFile);
```

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 ?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 21, 2014, 4:31pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/2 "2014-10-21T16:31:47Z")

</div>

This is the key line in the JUCE demo:

```
currentAudioFileSource = new AudioFormatReaderSource (reader, true);
```

The second argument (true) tell the&nbsp;AudioFormatReaderSource to take ownership of the reader and therefore delete it when itself is deleted.

---

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [October 21, 2014, 4:44pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/3 "2014-10-21T16:44:11Z")

</div>

I know. But why doesn't it work&nbsp;in MY code ?

&nbsp;

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 21, 2014, 4:48pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/4 "2014-10-21T16:48:00Z")

</div>

What objects are you leaking?

---

<div class="post-metadata">

**Author:** ![jrlanglois](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jrlanglois/32/21372_2.png) [@jrlanglois](https://forum.juce.com/u/jrlanglois)\
**Post date:** [October 21, 2014, 5:55pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/5 "2014-10-21T17:55:00Z")

</div>

Nevermind

---

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [October 22, 2014, 7:57am UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/6 "2014-10-22T07:57:57Z")

</div>

OK. Here is one way I found that seems to work. But I still don't understand why using Jules' clean code did not do it.

1 - Define&nbsp;currentAudioFileSource as a regular pointer (not a ScopedPointer)

2 - Define the InputStream as a regular pointer (not a ScopedPointer)

3 - Remove the following 3 lines from the loadFileIntoTransport method

```
transportSource.stop();
    transportSource.setSource (nullptr);
    currentAudioFileSource = nullptr;
```

4 - Use the following code in the caller:

```
transportSource.stop();
            transportSource.setSource (nullptr);
            delete currentAudioFileSource;
            audioFileInputStream = new MemoryInputStream (fileToRead, fileSize, false);
            loadFileIntoTransport(audioFileInputStream);
```

5 - Use the following code in the Destructor:

```
transportSource.setSource (nullptr);
    audioSourcePlayer.setSource (nullptr);
    audioSourcePlayer2.setSource (nullptr);
    delete currentAudioFileSource;
    delete midiFileInputStream;
```

\> No more leaks.

If anyone can explain why I needed to go through all this instead of using the same code as in the Juce Demo, I'm a taker...

:)

Phil.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 23, 2014, 9:49pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/7 "2014-10-23T21:49:49Z")

</div>

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&nbsp;  
_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&nbsp;_loadFileIntoTransport_ takes a pointer that is owned by something else sets off a few alarm bells. Why are you passing something called&nbsp;_fileToRead_ into a _MemoryInputStream_? It's impossible to tell from the snippets what the problem is, it doesn't show the whole picture.  
&nbsp;

---

<div class="post-metadata">

**Author:** ![phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phil](https://forum.juce.com/u/phil)\
**Post date:** [October 26, 2014, 12:44pm UTC](https://forum.juce.com/t/audioformatreader-leaks/13752/8 "2014-10-26T12:44:13Z")

</div>

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&nbsp;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.
