LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

postdeferredcall with callback data

Hello,

I have a function which is called by an asynchronous timer. So this runs in a seperate thread then my main function.
Now in this timer callback I use a PostDeferredCallback to trigger another callback and let it run in the main thread.
Now when I pass the void pointer with the callback data to the deferred function it works great in debug mode and not in release mode.
I already figured out that this is because the data where the void pointer is pointing to is already discarded because the function (of the asychronous timer) was already finished.
In debug mode the program runs a little slower and then the data is not yet discarded.

Now my question is: How can I solve this problem?
I could make my callback data a global variable but I don't really like this solution.
My callback data is a struct. Is there no way to pass the complete datastructure instead of a pointer?

Thanks for your help
0 Kudos
Message 1 of 3
(3,058 Views)
allocate the memory for your data on the heap (by using malloc()), then pass a pointer to the data.

e.g. (function signatures are clearly wrong, but you get the idea):
int timer_callback()
{
    DATA *data;

    data = malloc( sizeof( DATA ) );
    // fill the structure here...

    PostDeferredCallback( &deferred_callback, (void *)data );
}

and please, DO NOT FORGET TO FREE THE POINTER when you are finished using it in the deferred callback.

Message 2 of 3
(3,050 Views)
Works like a charm...

Thanks a lot !
0 Kudos
Message 3 of 3
(3,038 Views)