Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

memory leak?

Đang mở
#940 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 12 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
c
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với demo create_monitor_with_helpers được cung cấp và tái hiện vòng lặp trong khi quan sát malloc_stats(). Theo dõi quyền sở hữu và việc dọn dẹp đối với đối tượng cJSON, chuỗi được tạo và các lần cấp phát lặp lại, sau đó so sánh số byte đang được sử dụng qua các lần lặp lại. Công việc được xem là hoàn tất khi nguyên nhân đã được xác định và mọi bản sửa lỗi đều được xác minh bằng một kết quả bộ nhớ có thể tái hiện.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C
Star
13k
Fork
3.5k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của DaveGamble/cJSON

Tất cả issue của DaveGamble/cJSON

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.