有效内存区域内读取无效

时间:2012-06-14 10:25:02

标签: c pthreads valgrind

我正在开发一个带有Ptreads的简单OCR程序,但由于一些奇怪的原因,程序在一个简单的赋值时崩溃。

导致崩溃的行是一个简单的任务: int **y=(mask->imdata); 所有指针都没问题(在ma​​lloc'ed和传递给这个函数时检查它们是相同的)

Valgrind提供以下输出:

==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)

如您所见,报告的无效读取大小为4,在12字节的区域内偏移量为8,这怎么会导致段错误?

编辑:没有Pthreads(执行与pthreads_create()调用相同的功能)它没有任何问题。即使在运行一个线程时,它仍然会出现段错误。

1 个答案:

答案 0 :(得分:1)

我相信你可能在阅读它之前free正在阅读的内存,正如我所期望的那样,从Valgrind那里产生一个错误,就像上面那样(尽管不直接看到它是怎么回事)本身会导致崩溃。)

如果您正在使用free写入之前发布的内存,则分段错误可能来自程序中的其他位置。