About memory leaks using the JSON library

Source: Internet
Author: User
Tags valgrind

In the previous post, the code to convert the contents of data contained in struct pmtinfo into a JSON string return is the correct result, but it causes a serious memory leak, and the previous post link is as follows: http://my.oschina.net/ bambooli/blog/514946

using the Linux memory Leak tool valgrind to Memory Check

valgrind Introduction

Memcheck: This is the most widely used tool in Valgrind, a heavyweight memory checker that discovers most memory errors in development , For example: Using uninitialized memory, using freed memory, memory access, and so on. This is also the part that this article will focus on .

Command: Valgrind--tool=memcheck--leak-check=full--show-reachable=yes./test

where--leak-check=full refers to a full check for memory leaks,--show-reachable=yes is the place where memory leaks are shown,--trace-children=yes is the trace subprocess.

Test procedure:

#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main () {    printf ("\ n begin ... \ n");    int nnum = 0;    while (1)    {        usleep (100000);        Char *p = (char *) malloc (1024x768);        Char *pp = (char *) malloc (2048);        printf ("\ n p =%p, pp =%p \ n", p, pp);        Free (p);        Char *PPP = (char *) malloc (a);        printf ("\ n PPP =%p \ n", PPP);        nnum++;        if (nnum = =)        {break            ;        }    }    printf ("\ n end ... \ n");    return 1;}
Performvalgrind--tool=memcheck--leak-check=full--show-reachable=yes./test command

It can be seen from:

a) heap Summary (heapsummary):

the memory that is still in use when the program exits is 332800bytes , divided into $ a block ( that is , the time of malloc);

the total number of heap applications is - times, the number of releases is - times, total application memory 435200bytes

b) Next is the memory leak point

1) main.cpp:17 Row allocates memory up to 128000bytes, allocate memory size toconfirm memory loss /c10>128000bytes

2) main.cpp:14 Row allocates memory up to 204800bytes to confirm the loss of memory .

c) memory leak Summary:

leaking 332800bytes , in $ in a block.

memory leaks caused by using the JSON library

after executing the valgrind--tool=memcheck--leak-check=full--show-reachable=yes./test command


the first three red boxes indicate the memory error address, the last box indicates the number of errors;

resolving memory leaks after using the JSON library

according to the prompt error, look at the source is known, because the memory is not released caused by the memory leak, the source test.c modified:

#include <stdio.h> #include <string.h> #include "jansson.h" #include "upu_struct.h" #include "upu_proto_  Parse.h "///Pmtinfo"/////Nnum The number of incoming mtinfo structures///Plen-C structure converted to JSON structure string length//implementation function:// Converts data content contained in a struct pmtinfo into a JSON string return char* struct_to_json_n (mtinfo *pmtinfo, int nnum, int *plen) {json_t *object=nu ll;json_t *array=null;int I,size;char *result=null;char *curresult=null;array=json_array (); for (I=0;i<nNum;++i) { Object=json_object ()///structure Pmtinfo contains data content const char* pmoid=mt_get_moid (&pmtinfo[i]); Const char* pe164=mt_get_ e164 (&pmtinfo[i]); const char* Pprototype=mt_get_prototype (&pmtinfo[i]); const char* Pmttype=mt_get_mttype ( &pmtinfo[i]); const char* pmtstate=mt_get_mtstate (&pmtinfo[i]); const char* Pmtip=mt_get_mtip (&pMtInfo[i ]); const char* Pnuip=mt_get_nuip (&pmtinfo[i]); const char* Puserdomain=mt_get_userdomain (&pmtinfo[i]); const char* Pdevid=mt_get_devid (&pmtinfo[i]);//Convert the contents of the data contained in the structure Pmtinfo to JSONFormat json_t *pjsonmoid=json_string (pmoid); json_t *pjsone164=json_string (pE164); json_t *pjsonprototype=json_string ( Pprototype); json_t *pjsonmttype=json_string (Pmttype); json_t *pjsonmtstate=json_string (pMtstate); json_t *pJsonMtip =json_string (Pmtip); json_t *pjsonnuip=json_string (Pnuip); json_t *pjsonuserdomain=json_string (PUserdomain); json_t *pjsondevid=json_string (Pdevid);//Integrate the contents of the JSON structure into objectjson_object_set_new (object, "Moid", pjsonmoid); json_object_ Set_new (Object, "E164", pJsonE164); Json_object_set_new (object, "Prototype", Pjsonprototype); Json_object_set_new ( Object, "Mttype", Pjsonmttype), Json_object_set_new (object, "Mtstate", pjsonmtstate); Json_object_set_new (Object, " Mtip ", Pjsonmtip); Json_object_set_new (object," Nuip ", Pjsonnuip); Json_object_set_new (object," UserDomain ", Pjsonuserdomain); Json_object_set_new (object, "Devid", pjsondevid), int size1=json_object_size (object);p rintf ("[%d] : size1=%d\n ", i,size1);//convert this struct array into a JSON-formatted string Curresult=json_dumps (object, Json_preserve_order);p rintf (" CurREsult=%s\n ", Curresult); Jsonp_free (curresult);//Add this string to the JSON struct array json_array_insert_new (array,i,object);//json_ Decref (object);} Size=json_array_size (Array);p rintf ("size=%d\n", size); Result=json_dumps (Array,json_preserve_order);//jsonp_free (result); Json_decref (array); return result;    }int Main () {int i = 0;    int nlen = 0;    int nnum = 5;char *pjsontostring=null;    Mtinfo *pmtinfoarray = NULL;    Pmtinfoarray = (Mtinfo *) malloc (nnum * sizeof (mtinfo));    memset (Pmtinfoarray, 0, Nnum * sizeof (mtinfo)); Through the function mt_set_e164 set E164 number 0512111885621 ~ 0512111885625 to Structure Pmtinfoarray[n] mt_set_e164 (&pmtinfoarray[0], "051211 1885621 "); mt_set_e164 (&pmtinfoarray[1]," 0512111885622 "); mt_set_e164 (&pmtinfoarray[2]," 0512111885623 "); mt_set_e164 (&pmtinfoarray[3], "0512111885624");        mt_set_e164 (&       PMTINFOARRAY[4], "0512111885625"); Pjsontostring=struct_to_json_n (Pmtinfoarray, Nnum, &nlen);p rintf ("pjsontostring=%s\n", pJsonToString); Jsonp_free (pjsontostring); free (pmtinfoarray); return 0;} 

The bold part is the modification of the added content, and then the memory leak detection:

The memory leaks have been resolved by the graph.

About memory leaks using the JSON library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.