AudioApplication producing sine wave crashes when slider component added and made visible to its window

The following code is to produce a SineWave it works fine when i haven’t added and made the frequency slider visible in MainComponent constructor .
when not added a window pops up when i run the code and a continuous sine wave is produced.

But when i add it and make it visible a sine wave is produced for 2 sec and stops and no window appears at all.

My issue is i am trying to add the slider in the window but the window doesn’t pop up after running the code when i addAndMakeVisible the slider.

Any help on what i’m doing wrong would be greatly appreciated. Thank you.

My MainComponent.h file :

class MainComponent  : public juce::AudioAppComponent
{
public:
    MainComponent();
    ~MainComponent() override;
    void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
    void getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill) override;
    void releaseResources() override;
    void paint (juce::Graphics& g) override;
    void resized() override;

private:
    static constexpr int outputs{ 2 };
    SineWave sineWave[outputs];
    juce::Slider frequencySlider;
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

My MainComponent.cpp file :

MainComponent::MainComponent()
{
    addAndMakeVisible(frequencySlider);
    setAudioChannels(2, 2);
    setSize (800, 600); 
}

MainComponent::~MainComponent()
{
    shutdownAudio();
}

void MainComponent::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
{
    for (int i = 0; i <= outputs;i++) {
        sineWave[i].prepareToPlay(sampleRate);
    }
}

void MainComponent::getNextAudioBlock (const juce::AudioSourceChannelInfo& bufferToFill)
{
    bufferToFill.clearActiveBufferRegion();
    for (int ch = 0; ch < outputs;ch++) {
        auto* outBuffer = bufferToFill.buffer->getWritePointer(ch);
        for (int s = 0; s < bufferToFill.buffer->getNumSamples();s++) {
            outBuffer[s] = sineWave[ch].getNextSample(200.00f)*0.03f;
        }
    }
}

void MainComponent::releaseResources()
{

}

void MainComponent::paint (juce::Graphics& g)
{

    g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));

}

void MainComponent::resized()
{
  
}

The Main.cpp:

#include <JuceHeader.h>
#include "MainComponent.h"

class SineWaveEquationApplication  : public juce::JUCEApplication
{
public:
    SineWaveEquationApplication() {}
    const juce::String getApplicationName() override       { return ProjectInfo::projectName; }
    const juce::String getApplicationVersion() override    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed() override             { return true; }

    void initialise (const juce::String& commandLine) override
    {
        mainWindow.reset (new MainWindow (getApplicationName()));
    }

    void shutdown() override
    {
        mainWindow = nullptr; // (deletes our window)
    }
    void systemRequestedQuit() override
    {
        quit();
    }

    void anotherInstanceStarted (const juce::String& commandLine) override
    {
      
    }

        class MainWindow    : public juce::DocumentWindow
    {
    public:
        MainWindow (juce::String name)
            : DocumentWindow (name,
                              juce::Desktop::getInstance().getDefaultLookAndFeel()
                                                          .findColour (juce::ResizableWindow::backgroundColourId),
                              DocumentWindow::allButtons)
        {
            setUsingNativeTitleBar (true);
            setContentOwned (new MainComponent(), true);

           #if JUCE_IOS || JUCE_ANDROID
            setFullScreen (true);
           #else
            setResizable (true, true);
            centreWithSize (getWidth(), getHeight());
           #endif

            setVisible (true);
        }

        void closeButtonPressed() override
        {
          
            JUCEApplication::getInstance()->systemRequestedQuit();
        }
   private:
        JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
    };

private:
    std::unique_ptr<MainWindow> mainWindow;
};

START_JUCE_APPLICATION (SineWaveEquationApplication)

You have a mistake in your prepareToPlay method :

for (int i = 0; i <= outputs;i++)

That loops one over the sinewaves array and probably overwrites the memory of the frequencySlider component, causing undefined behavior. You need to use < there and not <=.

1 Like

thank you so much worked like a charm.