# Trouble with initialize

**URL:** <https://forum.juce.com/t/trouble-with-initialize/41540>\
**Category:** General JUCE discussion\
**Created:** [September 1, 2020, 5:33pm UTC](https://forum.juce.com/t/trouble-with-initialize/41540 "2020-09-01T17:33:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guendulainruiz](https://avatars.discourse-cdn.com/v4/letter/g/ed655f/32.png) [@guendulainruiz](https://forum.juce.com/u/guendulainruiz)\
**Post date:** [September 1, 2020, 5:33pm UTC](https://forum.juce.com/t/trouble-with-initialize/41540/1 "2020-09-01T17:33:39Z")

</div>

I downloaded a sourde code from github that converts images into wavetables, it’s from 3 years ago so i believe something change in the frame work… I’m getting this error:

> ‘initializing’: cannot convert from ‘std::unique\_ptr\<juce::FileOutputStream,std::default\_deletejuce::FileOutputStream\>’ to ‘juce::FileOutputStream \*’

this is the line where im getting the error

> FileOutputStream \*outputStream = outputFile.createOutputStream();

the whole if statement:

> ```
> if(fileDialog.browseForFileToSave(true))
> 
> ```

```
        {
            // Pipe audio data to a file
            File outputFile = fileDialog.getResult();
            int size = (int)AudioOutBuffer[0].size();
            AudioSampleBuffer* buffer = new AudioSampleBuffer(2,size);

            buffer->clear();

            CopyAudioToBuffer(buffer);

            WavAudioFormat* WavFile = new WavAudioFormat();

            FileOutputStream *outputStream = outputFile.createOutputStream();

            AudioFormatWriter* streamWriter = WavFile->createWriterFor(outputStream, 44100, 2, 16, NULL, 0);

            streamWriter->writeFromAudioSampleBuffer(*buffer, 0, size);

            delete streamWriter;
            delete buffer;
        }
    }

```

thanks!!

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 2, 2020, 6:37pm UTC](https://forum.juce.com/t/trouble-with-initialize/41540/2 "2020-09-02T18:37:20Z")

</div>

This is because a in the meantime a lot of functions that returned pointers that the caller must take ownership of changed their signature to return a `std::unique_ptr` instead of the raw pointer. This is the modern C++ way of managing object lifetime, as you cannot forget to call delete on such objects and create a memory leak this way. Looking at the code above I can see some leaks, which shows that unique ptrs are a good idea, especially for beginners 🙂 The code block above rewritten with those modern approaches would look something like, I’m fan of using `auto` a lot to avoid long type names

```auto
if(fileDialog.browseForFileToSave(true))
{
    // Pipe audio data to a file
    auto outputFile = fileDialog.getResult();
    auto size = static_cast<int> (AudioOutBuffer[0].size());
    // There is no real reason to allocate an audio buffer with new, its a container itself which can safely live on the stack
    AudioSampleBuffer buffer (2, size); 

    buffer.clear();

    // buffer was a pointer before, so dereferencing it here. Better: Modify CopyAudioToBuffer to take a reference instead of a pointer 
    CopyAudioToBuffer (&buffer);

    // Same as with the buffer above, just keep it on the stack
    WavAudioFormat wavFile;

    // File::createOutputStream also changed to return a unique pointer
    auto outputStream = outputFile.createOutputStream();

    // WavAudioFormat::createWriterFor has not been updated yet and returns a raw pointer. We should wrap it in a unique pointer
    // One special thing here: it this function will take ownership of the stream passed in in case it could create a writer.
    // As there is no error checking if creation worked at all this code assumes that it will always create a writer and will always release the stream
    std::unique_ptr<AudioFormatWriter> streamWriter (wavFile.createWriterFor(outputStream.release(), 44100, 2, 16, nullptr, 0));

    streamWriter->writeFromAudioSampleBuffer (buffer, 0, size);

    // When using unique pointers they will delete their content automatically here without calling delete
    }
}

```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [September 2, 2020, 8:36pm UTC](https://forum.juce.com/t/trouble-with-initialize/41540/3 "2020-09-02T20:36:39Z")

</div>

If you’re using c++17, you should do:

```auto
if( auto t = funcThatReturnsUniquePtr() ) 
{
    //Ok to use t as it holds a valid object
}

```

Do this for all of those functions in the example above that return unique\_ptrs.

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 2, 2020, 10:03pm UTC](https://forum.juce.com/t/trouble-with-initialize/41540/4 "2020-09-02T22:03:34Z")

</div>

That should be possible with c++14 as well, shouldn’t it?

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [September 3, 2020, 12:01am UTC](https://forum.juce.com/t/trouble-with-initialize/41540/5 "2020-09-03T00:01:23Z")

</div>

looks like `if( init statement )` is a C++17 thing.

[https://en.cppreference.com/w/cpp/language/if](https://en.cppreference.com/w/cpp/language/if)

---

<div class="post-metadata">

**Author:** ![kamedin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kamedin/32/11711_2.png) [@kamedin](https://forum.juce.com/u/kamedin)\
**Post date:** [September 3, 2020, 12:36am UTC](https://forum.juce.com/t/trouble-with-initialize/41540/6 "2020-09-03T00:36:50Z")

</div>

The declaration as condition is C++98. What’s new in C++17 is something like

```auto
if (auto a = something; a != whatever)

```

like a for without the iteration expression.
