Dynamic storage/Assignment of DSPs (C++, FMOD Ex)

Hello FMOD forum,

I’m stuck on something very simple. I have a function that creates a DSP unit and a new instance of a class, sets the dsp’s frequency, and pushes a pointer to the DSP onto a vector where all the DSP pointers are stored. Each object of the class is assigned the dsp’s name and index in the vector, and is also assigned a channel. During runtime, in an infinite loop I have a call to each instance of the class, telling it to turn the DSP it was assigned on or off if a certain condition is met.

Code is below. Disclaimer: I’m a novice and don’t understand how DSPs work.

//objects of my class are stored in vector objVec
//both vectors are declared globally (eep)

std::vector <Obj> objVec;
std::vector <FMOD::DSP *> dspVec;

/*newObj constructs an instance of the Obj class and a DSP, pushed the DSP onto dspVec*/

void newObj(FMOD::Channel** cn, FMOD::DSP* dspname, double freq, int dspindex){
    FMOD_RESULT result;
    Obj nObj = Obj( cn, dspname,  freq,  dspindex);

    result = Asystem->playDSP(FMOD_CHANNEL_REUSE, dspname, false, cn);

/* Obj class has functions getChannel() - which returns a pointer to a pointer to a channel - and getDspIndex(). */

void display() { //called continuously in infinite loop
    FMOD_RESULT      result;
    //opengl stuff

    for (int i = 0; i < objVec.size(); i++) {
        FMOD::Channel* currentchannel = *objVec[i].getChannel();
        FMOD::DSP* currentdsp = dspVec[(objlVec[i].getDspIndex())];
        if (condition)
        else if (another condition)

    //opengl stuff

All this doesn’t work. It produces sound for a moment and then crashes. For what its worth, if instead of setting the dsps Active and Inactive I just pause and unpause the channels, the sound works fine (although obviously that work-around has little value, since I’ll have many DSPs on each channel).

Please let me know if I should provide more info. Thanks for any responses!

From what I can see you created an object on the stack, pushed that onto a list, left the function meaning the stack object is de-scoped and now the object would be invalid memory.

You should dynamically allocate the object on the heap with a pointer, instead of the stack, and it would probably work.

Hi Brett, thanks for your response. I’m not exactly sure how to enact your change though: I tried making the vector of dsp’s hold pointers to pointers to dsps but I’m still running into trouble.
Could you by any chance explain with a couple lines of code? I’d really appreciate it.

where you had “Obj nObj = Obj” it should be allocating heap memory instead, like “Obj *nObj = new Obj”

I’m not sure this is the problem: I know that the Obj instances are successfully being pushed onto the objVec before your change, because the OpenGl portion of my code is running fine (i.e., the functions that call objVec successfully return the data at the indexed positions).
I think the problem has to do with the vector dspVec, which I now have holding pointers to pointers to dsps. Somehow, after I create the dsp, something is going wrong either when I try to push the pointer-to-a-pointer-to-it onto dspVec, or when I try to index the vector to perform functions on the stored pointer.

I’m pretty sure there’s a memory leak somewhere in my newObj function, having to do with the creation and allocation of these dsps. Is there some better way dynamically manage dsps?
Thanks again for your help with this.

Another issue I can see is you’re passing FMOD::DSP * to a function that requires FMOD::DSP **, ie createDSPByType should have “&dspname”, not “dspname”.

I’m not even sure how that compiles.