Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

memory leak?

Abierto
#940 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 12 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
c
Área
backend

Línea de trabajo

Comienza con la demo create_monitor_with_helpers proporcionada y reproduce el bucle mientras observas malloc_stats(). Rastrea la propiedad y la limpieza del objeto cJSON, la cadena generada y las asignaciones repetidas; después, compara los bytes en uso a lo largo de iteraciones repetidas. Se considera terminado cuando se haya establecido la causa y cualquier corrección se haya verificado con un resultado de memoria reproducible.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
13k
Forks
3.5k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de DaveGamble/cJSON

Todos los issues de DaveGamble/cJSON

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.