3

I'm writing an app for Tizen (smart watches). Can't pass a structure to a callback. My typedef:

typedef struct _Health {
    ...;
    const char *data;
} health_s;

Declaring a structure and initializing it:

health_s health = {
    ...,
    .data = "steps"
};

Passing it to a function:

_app_check_and_request_permission("http://tizen.org/privilege/healthinfo", &health);

Getting and checking if the struct address passed well:

void _app_check_and_request_permission(const char *privilege, void *user_data)
{
    health_s *p_health = user_data;
    dlog_print(DLOG_INFO, LOG_TAG, "Health data: %s", p_health->data);
    ...

And here I see in the log "Health data: steps". Passed well. Then I'm trying to pass user_data to a callback function

    ...
    ppm_request_permission(privilege, _app_request_response_cb, p_health);
}

From function description in Tizen docs:

...
* @param[in]   user_data   User specific data which will be passed to
*                          the given callback.
int ppm_request_permission(const char *privilege,
                           ppm_request_response_cb callback,
                           void *user_data);

Callback declaration:

static void _app_request_response_cb(ppm_call_cause_e cause, ppm_request_result_e result,
                             const char *privilege, void *user_data);

And in the callback I'm getting Health data: NULL. Tried to pass p_health->data to the callback and that's ok. The problem is with passing the whole structure. What am I doing wrong? Thanks. Update: Checked the addresses of the passed structure in the _app_check_and_request_permission function and in the callback and they are the same. But the struct in the callback is still empty...

OlegMalakhov
  • 53
  • 1
  • 6
  • Normally you try to pass *a pointer to* a structure ... – Mike Robinson Feb 14 '21 at 17:16
  • @user9160119 - Did you define `health` with static storage duration, or at least are you sure that the lifetime of `health` extends to the point of callback? – Armali Feb 14 '21 at 17:17
  • @Armali No, `health` isn't defined as static. Not sure about the lifetime of `health`. Will try to figure this out somehow. Thank you! – OlegMalakhov Feb 14 '21 at 17:23

1 Answers1

1

Probably the lifetime of health has ended before _app_request_response_cb is called by the system on completion of the request. This is, for example, the case if the shown definition of health is contained in a function which returns before the request completes. Simple remedy: define health with storage class static.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Thanks again! You're totally right! `health` was created in `app_create` function which finished before the callback. Defined `health` as static and it works fine. – OlegMalakhov Feb 14 '21 at 17:44