Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

memory leak?

Aperta
#940 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 12 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
c
Ambito
backend

Direzione di ricerca

Inizia con la demo create_monitor_with_helpers fornita e riproduci il ciclo osservando malloc_stats(). Traccia la proprietà e la pulizia dell'oggetto cJSON, della stringa generata e delle allocazioni ripetute, quindi confronta i byte in uso su iterazioni ripetute. Il lavoro è completato quando la causa è stata stabilita e qualsiasi correzione è stata verificata con un risultato di memoria riproducibile.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I write a demo test using cjson:
creating json object with array, the array size is rand, code is as follow.
i run the code, check heap size real-time, but i found,
in use bytes is increasing never decrease,
is memory leak? or i am wrong? any help, thanks!

`
/*

  • cJSON test examples
    */

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include <malloc.h>
#include <string.h>
#include <unistd.h>

//NOTE: Returns a heap allocated string, you are required to free it after use.
char *create_monitor_with_helpers(void)
{
const unsigned int resolution_numbers[3][2] = {
{1280, 720},
{1920, 1080},
{3840, 2160}
};
char *string = NULL;
cJSON *resolutions = NULL;
size_t index = 0;

cJSON *monitor = cJSON_CreateObject();

if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL)
{
	goto end;
}

resolutions = cJSON_AddArrayToObject(monitor, "resolutions");
if (resolutions == NULL)
{
	goto end;
}

int N = 1000;
int value = rand() % (N + 1);
printf("rand value %d\n", value);
//for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
for (index = 0; index < value ; ++index)
{
	cJSON *resolution = cJSON_CreateObject();

	if (cJSON_AddNumberToObject(resolution, "width", value) == NULL)
	{
		goto end;
	}

	if (cJSON_AddNumberToObject(resolution, "height", value) == NULL)
	{
		goto end;
	}

	cJSON_AddItemToArray(resolutions, resolution);
}

string = cJSON_Print(monitor);
if (string == NULL)
{
	fprintf(stderr, "Failed to print monitor.\n");
}

end:
cJSON_Delete(monitor);
return string;
}

/*

  • Main
    /
    int main() {
    char
    out;

    while(1){
    malloc_stats();

     out = create_monitor_with_helpers();
     printf("packing %ld char string\n", strlen(out));
     //printf("%s",out);
     free(out);
    
     sleep(1);
    

    }

    return 0;
    }
    `

runing about 5 minute, malloc_stats() output increasing to as follow:

Arena 0:
system bytes = 704512
in use bytes = 92304
Total (incl. mmap):
system bytes = 704512
in use bytes = 92304
max mmap regions = 0
max mmap bytes = 0

about 90K byte.

Lingua principale
C
Stelle
13k
Fork
3.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DaveGamble/cJSON

Tutte le issue di DaveGamble/cJSON

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.