Array in class causing exc_bad_access... pointer issue?

I’ve isolated an error to an array of floats I create in a class “sinOscillator”. When the class is instantiated by itself, it works just fine. When I create an array of the same class, I get an EXC_BAD_ACCESS error. If I remove the array of floats from the class, I do not receive the error.

I’m new to C++, but from poking around it sounds like a memory / pointer issue I don’t comprehend yet.

Here is the sinOscillator.h that has the offending array volumeEnvelope:

class sinOscillator{  
	void	setup();  
	void	setFrequency(float newFrequency);  
	void	setVolume(float newVolume);  
	void	addToSoundBuffer(float * buffer, int bufferSize);   
	float  volumeCurrent;  
	float  volumeTarget;  
	float  frequency;  
	float	* volumeEnvelope;  

…and the setup function that initializes the float array:

void sinOscillator::setup(){	   
	volumeCurrent	= 0.1f;	  
	volumeTarget	= volumeCurrent;  
	volumeEnvelope	= new float[88200];  
	for (int i = 0; i < 88200; i++) {  
		volumeEnvelope[i] = volumeCurrent;  

Again, when the sinOscillator class is used by itself, no problems. But if I make an array of sinOscillators, I get the EXC_BAD_ACCESS error. Without volumeEnvelope there is no error.

I’m guessing this is a pointer issue. What am I doing wrong?

Found the answer. The ‘Dynamic Arrays’ section of this-page explains what I was doing wrong.

Okay, I take it back. I’m still clueless here. I tried making constructors, but I can’t seem to get this right. Could someone explain why making single instances of my sinOscillator class would work, but making an array of them would consistently get me the EXC_BAD_ACCESS error?

Hard to tell from just this code, which by itself seems fine (although there’s no deleting of the array in a destructor - you’ll need that later). Are you calling setup for each instance of sinOscillator? Also, ensure you’re calling setup before using your volumeEnvelope array (for every sinOscillator you create).

Basically, ensure you’re creating the array before you use it, and do this for every sinOscillator you create.

I’ve attached the project (Xcode), and I’ve pointed out where I could get the error to happen. If you run the code without the vector of sinOscillators, it will work. If you run it with the vector, it will crash when it runs the setVolume() method. There is a block of code there that is messing things up.

What I don’t understand is why running the same setVolume() method doesn’t cause a crash when it’s done by itself, but when a vector of the same class runs the method it crashes.

Thanks in advance for any help!

the problem is vectors (and STL generally) is not thread safe at all, and the ofSoundStream is running in a separate thread linked to the sound card. If you alter / read / write a vector in two thread you will crash. in your code w/ the vector, you use the vector of sinOscillators in both the stream code and the main code, and thus the crash.

does that explanation help / make sense ?

take care!

try adding pointers of your class to the vector not the instance itself.

If you add an instance to a vector (or any STL container) a *copy* of that instance gets added to the vector, this involves a copy-constructor, which does not handle the array insider of your class. The compiler which creates a default copy constructor which works fine for PODs doesn’t know how to handle the volumeEnvelope, so it stays NULL.

There a two possibilities:

1.) implement a copy-constructor for sinOscillator which copies the volumeEnvelope
2.) create your sinOsillators on the heap and add a pointer to the vector.


While Sth has really good points, I still would think the problem is with vectors being non-thread safe — i think that even if the vector is of pointers and not the object, access (even reading) the vector in two threads (ie, using it in the sound stream and calling setVolume() in the main app thread) will cause crashing…

take care,

It makes more sense now. I’m assuming arrays have the same threading issue as vectors? When I load an array of sinOscillators I get the same crashing issue.

And if that’s the case… I’m not sure how to proceed. What is the proper way to use arrays in classes you intend to load into vectors / arrays?

I’m not sure – can you post a non vector code that crashes?
I’m very suspicious of vectors w/ thread, but I think arrays should be ok…
can you post something simple up?

take care,

  • zach

Sure - here it is:

It will compile and run as is. There is a block of code in the testApp.setup() that initializes the sinOscillator[0], and if you uncomment it it will throw the error. Thanks for taking a look.

Okay, I was missing something else. I was calling ofSoundStreamSetup() *before* I was initializing my arrays. So the audioRequested() function was being called before the setup was complete.

Learning process - hope this helps someone in the future.