0
0

Hi to all,
I tried to do a very simple operation to test passing data to a callback callback.
in the main program I did:

[code:2novd158]FMOD_DSP_DESCRIPTION desc;
char tmp[20] = "teststring\0";

memset(&desc, 0, sizeof(FMOD_DSP_DESCRIPTION))
..some config param
desc.channels = 0;
desc.read = &dspCallback;
desc.userdata = (void *)&tmp;

result = fmodSystem->createDSP(&desc, &recDSP);

result = fmodSystem->addDSP(recDSP, 0);
recDSP->setBypass(false);
recDSP->setActive(true);[/code:2novd158]

And in the callback (dspCallback) routine I did

[code:2novd158]FMOD::DSP *dsp = reinterpret_cast<FMOD::DSP *>(dsp_state->instance);
char *t = NULL;
dsp->getUserData((char *)&t);
printf(t);
return FMOD_OK[/code:2novd158]

When I run my program I would see at shell lots of teststring strings but I see lots of ascii caracteres that I don’t understand.
Where is the mistake I did?
Thank to all,
Franco

  • You must to post comments
0
0

You string (char tmp[]) is a stack variable. That means that the memory it occupies can be reclaimed as soon as the variable goes out of scope. If you want to pass data into a callback, you should put it on the heap (ie allocate it with malloc()/realloc()/calloc()/strdup()/new/new[]/etc.)

The reason you’re seeing those garbage strings is that your string is getting overwritten with some other stack data. Sometimes it is, and sometimes it isn’t.

  • You must to post comments
0
0

[quote="Adiss":jjjciypn]You string (char tmp[]) is a stack variable. That means that the memory it occupies can be reclaimed as soon as the variable goes out of scope. If you want to pass data into a callback, you should put it on the heap (ie allocate it with malloc()/realloc()/calloc()/strdup()/new/new[]/etc.)

The reason you’re seeing those garbage strings is that your string is getting overwritten with some other stack data. Sometimes it is, and sometimes it isn’t.[/quote:jjjciypn]

Thank you Adiss,
my purpose is extract the audio data from a dsp (as suggested by Tony), put them in a memory buffer and pass the buffer to an audio codec I wrote to compress the extracted data in an AVI file. The buffer is allocated outside the callback and passed to the callback as userdata to be filled. If I create it as suggested by you (with new or malloc) I need to reallocate it or only 1 time? If it goes out of scope I’ll loose the reference on it?
Regards,
Franco

  • You must to post comments
0
0

[quote="Adiss":3j12c3rm]You string (char tmp[]) is a stack variable. That means that the memory it occupies can be reclaimed as soon as the variable goes out of scope. If you want to pass data into a callback, you should put it on the heap (ie allocate it with malloc()/realloc()/calloc()/strdup()/new/new[]/etc.)

The reason you’re seeing those garbage strings is that your string is getting overwritten with some other stack data. Sometimes it is, and sometimes it isn’t.[/quote:3j12c3rm]

Adiss, I have a doubt. If I create the buffer on the heap with new/malloc I need to pass the pointer to the allocated memory to the callback and the pointer passed is always passed into the stack right? If so I have the same problem.
Can you post me some line of code as example please?
Regards,
Franco

  • You must to post comments
0
0

replace

[quote:17ldb11c]char tmp[20] = "teststring\0"; [/quote:17ldb11c]

with

[code:17ldb11c]char *tmp = new char[20];
strcpy(tmp, "teststring");[/code:17ldb11c]

Then the rest should work fine.

  • You must to post comments
0
0

[quote="francoAmato":3tynxj3q]Adiss, I have a doubt. If I create the buffer on the heap with new/malloc I need to pass the pointer to the allocated memory to the callback and the pointer passed is always passed into the stack right? [/quote:3tynxj3q]

No it is not, the address of the heap memory is passed to the function.
The stack is irrelevant in this case.

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.