Menu

cJSON_Delete pointer error

Omkar
2014-10-13
2014-10-13
  • Omkar

    Omkar - 2014-10-13

    I have a piece of code as follows using the cJSON library

    const char jSONSkeleton[] = "{\"name\": \"\",\"address\":{\"streetName\":\"\",\"houseNo\":0}}";
    cJSON json= cJSON_Parse((char )jSONSkeleton);
    cJSON *format = cJSON_GetObjectItem(json,"address");

    cJSON_GetObjectItem(json,"name")->valuestring = "Mike";
    cJSON_GetObjectItem(format,"streetName")->valuestring = "Jump Street";
    cJSON_GetObjectItem(format,"houseNo")->valuedouble = 21;

    char *output = cJSON_Print(json);
    printf("PERSON DETAILS: %s\n", output);
    cJSON_Delete(json);

    The delete operation seems to throw an invalid pointer error. Does anyone know why this might be happening and a workaround this? I am new to this library.

     
    • Dave Gamble

      Dave Gamble - 2014-10-13

      Hi,

      On 13 Oct 2014, at 17:00, Omkar omkarhegde@users.sf.net wrote:

      I have a piece of code as follows using the cJSON library

      const char jSONSkeleton[] = "{\"name\": \"\",\"address\":{\"streetName\":\"\",\"houseNo\":0}}";
      cJSON json= cJSON_Parse((char )jSONSkeleton);
      cJSON *format = cJSON_GetObjectItem(json,"address");

      cJSON_GetObjectItem(json,"name")->valuestring = "Mike";
      NOPE.

      cJSON_GetObjectItem(format,"streetName")->valuestring = "Jump Street";
      NOPE.

      cJSON_GetObjectItem(format,"houseNo")->valuedouble = 21;
      NOPE.

      char *output = cJSON_Print(json);
      printf("PERSON DETAILS: %s\n", output);
      cJSON_Delete(json);

      The delete operation seems to throw an invalid pointer error. Does anyone know why this might be happening and a workaround this? I am new to this library.

      You sure are! Crikey. Not sure this is the right library for you. JSON is a serialisation format. You don't parse back/forth like that. You create your output structure in one hit and serialise.

      Maybe if you did something like:

      cJSON json=cJSON_CreateObject();
      cJSON
      format=cJSON_CreateObject();
      cJSON_AddItemToObject(json,"name",cJSON_CreateString("Mike"));
      cJSON_AddItemToObject(json,"address", format);
      cJSON_AddItemToObject(format,"streetName",cJSON_CreateString("Jump Street"));
      cJSON_AddItemToObject(format,"houseNo",cJSON_CreateNumber(21));
      char *output = cJSON_Print(json);
      printf("blah blah blah... %s",output);
      cJSON_Delete(json);

      that'd get what you wanted.

      You build up structure as you go. Don't try modifying the cJSON structure unless you really know what you're doing. That's not what it's for. It's a parser/printer, not a general-purpose-I-will-psychically-guess-the-lifetime-of-your-data management system. It's designed to be fast and simple, not feature-rich, and certainly not something that will manage data ownership by magic like that.

      Dave.

      cJSON_Delete pointer error

      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/cjson/discussion/998969/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       

Log in to post a comment.