I have been having trouble getting my program to free memory correctly. I believe the problem occurs when I try and free the memory of the block array. I figured out how to free the memory for the name of the jpeg (fileName). Whenever I free the memory in the spaces I commented out, I get an "Abort (core dumped)" after a backtrace and memory map. The program compiles and runs correctly as long as I do not try to free the memory of the block array.

[MOD: Removed the code as I found the solution]

Valgrind outputs that I have 5280 bytes in 15 blocks at exit. It also says that I have done 33 allocs and 34 frees. This only occurs when I have the free(block) in use. Otherwise it says the same 5280 bytes in 15 blocks, but 33 allocs and 18 frees.


I'm thinking that you're chasing a red herring.

From the documentation: "The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc."

If you're not allocating it, don't free it.

I also see that you're not closing the current output file before opening a new one using the same pointer. You do close the remaining output file at the end though. Valgrind reports 15 blocks, and you're not closing 15 files. Maybe there's a clue there?

I'm also totally baffled why you are using freopen()?

If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)

  • I actually just recently changed the program to do reopen. I saw it in one of the suggested topics to research when i typed in the title of the post. But the file closing may be the issue. I'm going to update the post with what I had before the freopen(). I still had the same problem when I was using fclose() – godspire Oct 6 '15 at 17:41
  • I actually looked at how i was doing it the first time and found the error. I was closing the file at the wrong time. Thank you so much! – godspire Oct 6 '15 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .