Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

memory leak?

オープン
#940 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 12 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
c
領域
backend

調査の方向性

提供されている create_monitor_with_helpers デモから始め、malloc_stats() を観察しながらループを再現します。cJSON オブジェクト、生成された文字列、繰り返し行われるアロケーションについて、所有権とクリーンアップを追跡し、その後、繰り返しの各イテレーションで使用中のバイト数を比較します。原因が特定され、修正が再現可能なメモリ結果で検証されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C
スター
13k
フォーク
3.5k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

DaveGamble/cJSON のほかの issue

DaveGamble/cJSON の issue をすべて見る

似ている issue

C の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。