MidiMessageSequence as class member?


Hi everyone,

So currently I’m teaching myself C++ by creating a small program in JUCE. However, I’ve run into a weird issue which I can’t seem to figure out. I have the following header-file:

#include "../JuceLibraryCode/JuceHeader.h"

class A
    A(MidiMessageSequence mms, double start, double end, double duration, String n);
    void function(std::vector<MidiMessage>& midiMessages, double start, double end);
    MidiMessageSequence midiSequence;

and the following cpp-file:

A::A(MidiMessageSequence mms, double start, double end, double duration, String n) : Content(start, end, duration, n) {
    midiSequence = MidiMessageSequence(); //Doesn't matter whether this is run or not

A::~A() {

void A::function(std::vector<MidiMessage>& midiMessages, double start, double end){
    midiSequence.addEvent(MidiMessage::noteOn(1, 50, 0.5f), 1); //Does run if placed in the constructor

And when running this code I continuously get the exception (EXC_BAD_ACCESS). The strange part is that this does not happen if I A) run the entire code in the constructor or B) run the code on a temporary MidiMessageSequence I just created instead of the object member called midiSequence. This leads me to believe that I’m not fully understanding the memory model that is used in C++ (maybe something with the pointers/references inside MidiMessageSequence?), so can anyone help me out here?

Thanks a lot already!


At least your paste has the class names messed up. In the header you have class A and in the cpp file you have MidiContent. If that’s just a mistake when pasting here, how about how is the A or MidiContent object itself created? Are you trying to use it as a heap allocated object? If yes, do you actually “new” it or just have a null/invalid pointer to it?