It’s difficult to tell without seeing a bit more of your code (like the .h files) but I’m guessing it’s because in Sample::play(), you’re dereferencing your myData pointer without checking if it’s NULL first. That’d be this line :
short* buffer = (short *)myData
So (again, guessing) it seems that the crash happens because:
Your sample finishes
You delete the sample’s data
You load new sample data
And, in between steps 2 and 3, Sample::play() happens and tries to access data that doesn’t exist yet. There’s a couple ways around this. The simplest fix seems like it would be to use your isPlaying bool to indicate if myData is ready. So, change :
Or, check if myData is != NULL before doing anything in play(). You’ll probably also want to write 0’s in play() if myData isn’t ready, but that’s up to you.
No problem! Just FYI here’s a couple things you’ll probably need to know when doing the sort of thing you’re doing.
About the error log, these are the important bits:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Basically, this means your program tried to access memory that wasn’t valid (it can mean other things too, but it’s almost always a case of reading from pointers that point to bad data).
Crashed Thread: 2
This tells you that your program crashed on thread 2 (ok that one was obvious). But, that tells you to take a look at thread 2’s stack trace, which looks like:
The top of the trace shows you that the program crashed in Sample::play(double), which should give you enough info to start figuring out your problem.
The other thing is that (if the problem is actually the issue I mentioned) you should look into what a mutex is and why you’d want to use one. OpenFrameworks comes with one called ofMutex that’s quite easy to use. The reason in this case is that you’re sharing data between two threads: the “main” thread which is doing your sample loading, and another thread which is controlling audio playback.