cJSON, MQTT and Tasks... extrange behavior

Hi everybody. I’m experiencing a really extrange behavior (for which I have a workround) but I will like to understand it because I must be missing somthing.

I have a cJSON struct called Config to store all my config parms. I declare, and initialize it in my main.c file. I’ve read that to use safely in a multitask enviroment I have to use a Semaphore so I also declare an initialize it in main.

The I use the mqqt_example.c file. In that file I declare Config an ConfigMutex as extern.

extern cJSON *Config;
extern SemaphoreHandle_t ConfigMutex;

I’ve created a “safe_get” function to handle the semaphore

char *safe_get(char *nombre)
xSemaphoreTake(ConfigMutex, portMAX_DELAY);
cJSON *caItem = cJSON_GetObjectItem(Config, nombre);

if (caItem) {
printf(“Getting %s \n”,nombre);
if (DEBUG)
printf(“DEBUG: %s: .%s. \n”, nombre, caItem->valuestring);
printf(“%s: Not found\n”, nombre);
return NULL;
return caItem->valuestring;

well, here comes the extrange thing . If I use this:

connectData.username.cstring = safe_get(“MQTT_USER”);

All my debug printf are correct, but user sent to MQTT server is incorrect.
but, if I do this instead:

char user_name[30];

char *tmptxt=safe_get(MQTT_USER);
connectData.username.cstring = user_name;

It works

can someone explain whats the difference??