I have my buffer as a long string from a file. Then i add word for word into a binary tree as nodes.
If i try to free my buffer when its no longer used, i get a Segmentation fault. Valgrind points out a lot of "Invalid read of size 1" errors.
If i move it down to my cleaning bracket. it works, but i get a serious memory leak (as the program can read more files).
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
}
Each node will allocate memory for itself inside the trees insert function.
 
     
    