pass int to a C callback
Why i is not recognized correctly in the callback?
I think that maybe because after add_strip() "i" is destroyed, so how could i pass an int to that callback? Thanks.
29 void add_strip(int i,char name[30]){
30 sl[i] = elm_slider_add(win);
31 elm_slider_label_set(sl[i], name);
32 elm_slider_unit_format_set(sl[i], "dB");
33 elm_slider_span_size_set(sl[i], 60);
34 evas_object_size_hint_align_set(sl[i], 0.5, EVAS_HINT_FILL);
35 evas_object_size_hint_weight_set(sl[i], 0.0, EVAS_HINT_EXPAND);
36 elm_slider_indicator_format_set(sl[i], "%3.0f");
37 elm_slider_min_max_set(sl[i], 0, 2);开发者_运维问答
38 elm_slider_inverted_set(sl[i], 1);
39 elm_slider_value_set(sl[i], 0);
40 elm_object_scale_set(sl[i], 1.0);
41 elm_slider_horizontal_set(sl[i], 0);
42 elm_box_pack_end(bx, sl[i]);
43 evas_object_show(sl[i]);
44 evas_object_smart_callback_add(sl[i], "changed", vol_changed, &i); // <--------------
45 }
And the callback is:
13 static void
14 vol_changed(void *data, Evas_Object *obj, void *event_info)
15 {
16 int n = *((int*)data);
17 printf("%d\n", &n); // <------------------------------------- this prints always -1078364196 (seems an address)
18
19 if(lo_send(dest, "/fader/0", "f", elm_slider_value_get(sl[0]))==-1)
20 printf("OSC error %d: %s\n", lo_address_errno(dest), lo_address_errstr(d est));
21 }
You are passing the address of a local variable as argument to a callback. So when the function exit the variable is deallocated. Its address is now invalid. When the callback is called, the address of your local variable is most probably used by something else, which explained the weird (and different) value.
Also, as noted in other post, you mistyped you snipset.
I don't quite get what your code is doing, but:
int n = *((int*)data);
printf("%d\n",i);
i
is not defined here; I assume the code accesses some global variable or somesuch. The integer you are retrieving from *data
is n
, not i
...
Your code should work as you've posted it ... at least it does for me (I've also posted it to codepad):
#include <stdio.h>
int callback(void *x) {
int n = *(int*)x;
printf("int in callback: %d\n", n);
return 0;
}
int driver(int (*fx)(void*), int x) {
printf("int in driver: %d\n", x);
fx(&x);
return 0;
}
int main(void) {
driver(callback, 42);
return 0;
}
There must be something else messing it up!
16 int n = ((int)data); 17 printf("%d\n", &n); // <------------------------------------- this prints always -1078364196 (seems an address)
Yes, it is an address. Maybe you would be more comfortable viewing it in hexadecimal. printf("0x%x", &n)
Where are you using i
in the callback? In anycase the scope of a function arguments is the function itself.
What I'd do is this
void add_strip(int *i,char name[30])
{
if(NULL == i){
//error condition, return or set some error no or whatever you fancy.
}
blah blah blah... (use *i)
evas_object_smart_callback_add(sl[*i], "changed", vol_changed, i);
}
So the callback would have a reference to "i" from the function which called add_strip. It is left to you to make sure that the variable "i" is not out of scope.
精彩评论