handleIncomingMidiMessage producing "EXC_BAD_ACCESS" error

Hey all,
So I’m running into a problem instantiating a class that handles midi messages as a midiInputDeviceCallback. I’m writing a command line program, and what I’m finding is that when I instantiate my midi receiving class (let’s call it a “pipe”) from the main function, it operates just fine, but when I instantiate my “pipe” class from within the method of another class I’m getting an “EXC_BAD_ACCESS” error from the “handleIncomingMidiMessage” function.

At first I figured this must be because the instantiated “pipes” were getting deleted at the end of the function that creates them (I’m new to C++ so there’s a lot I’m still getting my head around), so I started using “emplace_back” at their instantiation in order to store them inside a vector member of the class who’s method instantiates it. This seems to prevent them from being destroyed, but I still get the error.

My best guess is that it has something to do with the deviceManager not being able to find the “pipe” that designated itself as the midiInputDeviceCallback? No idea why or how to fix it though.

Can anybody help me work out what’s happening here?

Thank you,

It’s usually impossible for anyone to provide help without seeing code. Please include code examples, surrounded by ‘triple tics’ ```

void yourCodeExampleWillLookLikeThis()

If you’re building with Xcode, try enabling Address Sanitizer (ASAN). You can do this by clicking on your app’s name in the title bar next to the run/stop buttons, selecting ‘Edit Scheme’, then clicking ‘Diagnostics’, then selecting the ASAN checkbox.

ASAN will give you a detailed report any time a memory-related issue (like an EXC_BAD_ACCESS) occurs, which might help you track down the problem. The diagnostic output will normally be printed to the ‘console’ window inside Xcode.

I’m going to guess you have a vector of objects, and that you are registering callbacks on these object? The problem with storing them in a vector is that if the vector resizes, the objects will be moved to different addresses, and the callbacks will point to the wrong place.

The address sanitiser option is a good call and might help track this down. The only really hard part of C++ is understanding object lifecycles and the various ways you can shoot yourself in the foot, and it sounds like you’ve just discovered one of them!

1 Like

Hey cpr, thanks for responding. I figured that may be the response, the code is just quite dense and it would be a task to reproduce it here.

Thank you reuk as well, that’s a good thing to know and I will definitely explore that.

I finally got my issue resolved, and while the code itself is too big to try to show here, here’s my best explanation of the problem for anyone else running into something similar:
I had a primary container that was instantiated at the beginning of my program (let’s call it a “Set”), that would then create several “Patch” objects which would each contain their own vectors of “Pipe” objects. My problems centered around how the Patches and Pipes were being instantiated, and whether or not they were copied or moved at instantiation. I was originally trying to avoid using “new” to instantiate them as the potential for accidentally leaving them undeleted made me nervous. I ended up just using unique pointers instead and that seems to have resolved my issues.

Don’t know if that’s useful to anyone as an explanation, but there it is.

Thanks guys!

Thanks cesare, just saw your reply.
That’s very much the heart of what I was running up against. Fortunately I got it worked out. And I sure learned something along the way!