CrashReport because of invalid memory address

Hey all,
I am trying to create multiple classes to initialize synths and pass one of them a class that would let me play the sounds using a MIDI keyboard.
I defined the code as below. But it crashes and reboots after receiving the MIDI command, and it should be because of my lack of knowledge of C++.

I am using Teensy 4.1 on PlatformIO and this is my first teensy project. Could you help me understand what the problem is?

I put all audio components in audio_setup and here is part of the code:
C++:
AudioSynthWaveform    lead_waveform1;
AudioSynthWaveform    lead_waveform2;
....
AudioConnection          patchCord1(lead_waveform2, 0, lead_mixer, 1);
....
void setupAudio() {
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.32);
  AudioMemory(40);
}

Here is part of the main.cpp

C++:
#include "lead_synth.h"
#include "audio_setup.h"
#include "play_mode.h"

...
void setup()
{
    Serial.begin( 115200 );
    setupAudio();

    LeadSynth lead_synth;
    lead_synth.setup();

    PlayMode play_mode;
    play_mode.setSynth(lead_synth);
    play_mode.setup();
}

And here you can see the play mode class
C++:
#ifndef PLAY_MODE_H
#define PLAY_MODE_H
#include <Audio.h>
#include "synth.h"

class PlayMode {
public:  
  PlayMode();
  void setup();
  void loop();
  void setSynth(Synth& synth);
  static void printBytes(const byte *data, unsigned int size);
  
private:
  static Synth* synth;
  static void myNoteOn(byte channel, byte note, byte velocity);
};
extern PlayMode play_mode;

#endif

C++:
#include "play_mode.h"
#include <Audio.h>

#include <USBHost_t36.h>
#include <EEPROM.h>
#include "settings.h"
#include "audio_setup.h"
Synth* PlayMode::synth = nullptr; 

PlayMode::PlayMode() {}

USBHost myusb;
USBHub hub1(myusb);
MIDIDevice midi1(myusb);

void PlayMode::setSynth(Synth& synth){
  this->synth = &synth;
}

void PlayMode::setup() {
  delay(1500);

  delay(10);
  myusb.begin();

  
  midi1.setHandleNoteOn(myNoteOn);
  Serial.println("USB Host InputFunctions setup done");
}

void PlayMode::loop() {
  myusb.Task();
  midi1.read();
}


void PlayMode::myNoteOn(byte channel, byte note, byte velocity) {
  if (synth != nullptr) {
      synth->oscPlay(40);
    }
  }

  void PlayMode::myNoteOff(byte channel, byte note, byte velocity) {
    synth->onNoteOff(channel, note, velocity);
  }
}


Code:
CrashReport:
  A problem occurred at (system time) 11:22:6
  Code was executing from address 0x0
  CFSR: 1
    (IACCVIOL) Instruction Access Violation
  Temperature inside the chip was 34.01 °C
  Startup CPU clock speed is 600MHz
  Breadcrumb #2 was 2223231875 (0x8483D383)
  Breadcrumb #4 was 20500032 (0x138CE40)
 
Please post a complete sketch (every piece and every part - e.g. with the minimal snippets that you have posted, we do not have enough info to see what is going on in LeadSynth) so that someone else can reproduce the problem.

Mark J Culross
KD5RXT
 
PlayMode::myNoteOn is a member function so you can't use it as a callback like that... I'm surprised it even compiles without an error.

Edit: my mistake, didn't see that it was static... you've declared the synth object inside setup(), as soon as that function ends that object is out of scope/destroyed but you're still keeping a pointer to it, by the time myNoteOn tries to use it the memory will be overwritten.
 
Last edited:
Again not much one can do, without more code, but one thing I noticed:

Code:
void PlayMode::myNoteOn(byte channel, byte note, byte velocity) {
  if (synth != nullptr) {
      synth->oscPlay(40);
    }
  }

  void PlayMode::myNoteOff(byte channel, byte note, byte velocity) {
    synth->onNoteOff(channel, note, velocity);
  }
in the NoteOn... you check for nullptr, but in the off you do not.
 
Back
Top