ファイルからの長い文字列としてバッファを持っています。次に、単語ごとに二分木にノードとして追加します。
バッファが使用されなくなったときにバッファを解放しようとすると、セグメンテーション エラーが発生します。Valgrind は、多くの「サイズ 1 の無効な読み取り」エラーを指摘しています。
クリーニングブラケットに移動すると。動作しますが、重大なメモリ リークが発生します (プログラムはより多くのファイルを読み取ることができるため)。
if (argc < 2) {
printf("No arguments was passed to proccess.\n");
return EXIT_SUCCESS;
} else {
for (int i = 1; i < argc; i++) {
FILE *file = openFile(argv[i]);
if (file != NULL) {
validFiles++;
// Get size of file and setting up a buffer
int fileSize = getFileSize(file);
buffer = calloc(fileSize + 1, sizeof(char));
fread(buffer,sizeof(char),fileSize,file);
fclose(file);
// Break down the buffer into a tree of nodes
pch = strtok (buffer,delimiters);
while (pch != NULL) {
root = insert(root, pch, argv[i], 1);
pch = strtok (NULL, delimiters);
} // free(buffer); should be here
}
}
}
if (validFiles > 0) {
searchBook(root);
freeTree(root);
free(buffer); // Only frees one of the buffers when multiple files are sent in
}
各ノードは、ツリー挿入関数内でそれ自体にメモリを割り当てます。