In a JNI .cpp file I have a struct with a SoundTouch* (SoundTouch is a C++ audio processing that I am wrapping for use in an Android project) and I initialize a vector of the structs as global objects like this:
struct SoundTouchExt
{
SoundTouch* sTouch;
queue<signed char>* fBufferOut;
int channels;
int sampleRate;
float tempoChange;
int pitchSemi;
int bytesPerSample;
SoundTouchExt()
{
sTouch = new SoundTouch();
fBufferOut = new queue<signed char>();
}
};
const int MAX_TRACKS = 16;
vector<SoundTouchExt> sProcessors(MAX_TRACKS);
This works, at least if I only use one of the SoundTouchExt objects at a time in my program (that's sort of a different story, but possibly related - with multiple instances in play causes distorted output).
However, if I declare it like this SoundTouch sTouch;
, comment out the new
and change the use of it accordingly ( ->
to .
), pointers to references, I compile fine but I get a FAULT 11 (seg fault) as soon as the program trys to use the object.
Here's where that happens:
...
SoundTouchExt& soundTouch = sProcessors.at(track);
setup(soundTouch, channels, samplingRate, bytesPerSample, tempo, pitchSemi);
}
static void setup(SoundTouchExt& soundTouch, int channels, int sampleRate, int bytesPerSample, float tempoChange, float pitchSemi)
{
SoundTouch& sTouch = soundTouch.sTouch;
soundTouch.channels = channels;
soundTouch.sampleRate = sampleRate;
soundTouch.bytesPerSample = bytesPerSample;
soundTouch.tempoChange = tempoChange;
soundTouch.pitchSemi = pitchSemi;
sTouch.setSampleRate(sampleRate);
sTouch.setChannels(channels);
...
}
With a little research, I'm thinking this could be an instance of the static intialization order fiasco. I don't see any global variables in the library source code, but I don't know enough about C++ to know what else to look for.
What can my observation suggest about the library (or maybe I'm not doing something correctly)?