I'm working on a simple OCR program with Ptreads, but for some weird reason the program crashes on a simple assignment.
The line causing the crash is a simple assignment:
int **y=(mask->imdata);
all pointers are okay (checked when malloc'ed and when passed to this function and they are the same)
Valgrind gives the following output:
==28948== Thread 2:
==28948== Invalid read of size 4
==28948== at 0x804913A: PearsonCorrelation (ocr_pool.c:239)
==28948== by 0x8049342: PearsonCorrelator (ocr_pool.c:275)
==28948== by 0x80497BF: characterSegmentation (ocr_pool.c:407)
==28948== by 0x8049A33: lineSegmentation (ocr_pool.c:489)
==28948== by 0x8049D64: doPage (ocr_pool.c:575)
==28948== by 0x4076954: start_thread (pthread_create.c:300)
==28948== by 0x41565ED: clone (clone.S:130)
==28948== Address 0x49d18f8 is 8 bytes inside a block of size 12 free'd
==28948== at 0x4023B6A: free (vg_replace_malloc.c:366)
==28948== by 0x80489ED: freeImage (ocr_pool.c:77)
==28948== by 0x8049F33: main (ocr_pool.c:610)
As you see, the reported invalid read is of size 4, with an offset of 8 in a region of 12 bytes, how can this be the cause of a segfault?
Edit: Without Pthreads (executing the same function as pthreads_create() calls) it works without any problems. Even when running one thread, it still gives a segfault.
I believe you are probably freeing the memory you're reading from before reading from it, as I would expect that to produce an error from Valgrind like the one above (though don't directly see how that in itself would result in a crash.)
The segmentation fault could be coming from somewhere else in your program if you're writing to memory you've previously released using free.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With