Correctly initializing EditViewState

Hi everyone

I have been struggling with this for a minute now, below is an example of what I’m trying to do. I get ‘editViewState is null’ on the console. How do I fix this so that track is not nullptr?

FooComponent::FooComponent(te::SelectionManager& sm, EditViewState& evs) : editViewState(evs), selectionManager(sm) 
{
	// Some code
}

void FooComponent::sliderValueChanged (Slider* slider)
{
//    if (slider == &volume)
//    {
//        editViewState.selectionManager.selectOnly (selectedTrack.get());
//
//        if (selectedTrack != nullptr)
//        {
//            if (auto at = dynamic_cast<te::AudioTrack*> (selectedTrack.get()))
//            {
//                if (auto* volumePlugin = at->getVolumePlugin())
//                {
//                    volumePlugin->setVolumeDb((float)volume.getValue());
//                }
//            }
//        }
//
//    }
    if (&editViewState != nullptr)
    {
        DBG(editViewState.selectionManager.getNumObjectsSelected() << "track selected");
    }
    else
    {
        DBG("editViewState is null");
    }
}

I have track and editViewState instantiated as below on the header file

private:
    EditViewState& editViewState;
//    te::Edit& edit;
    te::SelectionManager& selectionManager;
//    EditViewState editViewState { edit, selectionManager };
    te::Track::Ptr selectedTrack;

I am trying to be able to access the volume plugin of the selected track and be able to adjust it with the slider on my FooComponent, what am I doing wrong?

When I uncomment the commented code on the sliderValueChanged I get a breaking point on this line:

editViewState.selectionManager.selectOnly (selectedTrack.get());

and when I hover a mouse over editViewState I get

‘(EditViewState &) editViewState = 0x0000000000000000’

The code is a bit hard to read, can you surround it with three backticks so it formats as code please?

But it’s not clear where you’re initialising the EditViewState& editViewState;, it looks like it’s just an initialised reference?

Have you copied this from one of the examples?

Thank Dave for a quick response, I am trying to modify the MidiRecodingDemo i created a FooComponent which has a slider that should control the volume of a selected track. I will try make the code readable im on a mobile phone

Right, but how do you construct FooComponent? I.e. what arguments do you pass to it?

Hi Dave, i have edited the code above to make it readable. The FooComponent has selectionManager and EditViewState as arguments

Right, but what do you pass to FooComponent when you construct it?

There’s a fair bit wrong with your example code. In C++, a reference can never be null so the if you have there could just be optimised away.

I would suggest you also run with AddressSanitizer enabled as that might help point you at where your problems start, at the moment you’re just seeing where they end.

1 Like

@dave96 ok, after reading your response a couple of times i think i finally understand what you mean. There were a couple of errors I made with my code which i finally got it right. Thank you again :slightly_smiling_face:

:+1:

1 Like