我正在尝试实现一个基本的循环缓冲区,然后在所述缓冲区中生成/使用几个字符。由于多个线程将访问同一个缓冲区,我还使用信号量来防止并发访问。我的程序编译并正确运行,但是,一旦我将一个字符传递给它,它就会导致分段错误。我一直在尝试使用 Valgrind 进行调试,但除了错误在于我的缓冲区代码(如下所示)之外,我无法从中收集到很多用途:
[...]//headers
void createBuffer(Buffer *buff, int buffSize){
buff = (Buffer*) calloc(1, sizeof(Buffer));
semaphore mutex,emptyBuffers,fullBuffers;
createSem(&mutex, 1);
createSem(&emptyBuffers,buffSize);
createSem(&fullBuffers,0);
(buff->mutex) = &mutex;
(buff->emptyBuffers) = &emptyBuffers;
(buff->fullBuffers) = &fullBuffers;
buff->charBuff = malloc(sizeof(char) * buffSize);
buff->nextIn = 0;
buff->nextOut = 0;
buff->buffSize = buffSize;
}
void deposit(char in, Buffer *buffer) {
down(buffer->emptyBuffers); //line 20 in buffer.c
(buffer->charBuff)[buffer->nextIn] = in;
buffer->nextIn = (buffer->nextIn + 1) % buffer->buffSize;
up(buffer->fullBuffers);
}
[...]//remove function
之后的 remove 函数类似于 deposit(),所以弄清楚如何修复一个应该足以让我弄清楚如何在必要时修复另一个。这是 Valgrind 的输出:
==10846== Use of uninitialised value of size 8
==10846== at 0x4014EF: deposit (buffer.c:20)
==10846== by 0x40124F: getInputStream (test.c:80)
==10846== by 0x401C55: _st_thread_main (in a.out)
==10846== by 0x401D1D: st_thread_create (in a.out)
==10846==
==10846== Invalid read of size 4
==10846== at 0x40167A: down (in a.out)
==10846== by 0x4014FA: deposit (buffer.c:20)
==10846== by 0x40124F: getInputStream (test.c:80)
==10846== by 0x401C55: _st_thread_main (in a.out)
==10846== by 0x401D1D: st_thread_create (in a.out)
==10846== Address 0xc74800405020c0c7 is not stack'd, malloc'd or (recently) free'd
==10846==
==10846== Process terminating with default action of signal 11 (SIGSEGV)
==10846== General Protection Fault
==10846== at 0x40167A: down (in a.out)
==10846== by 0x4014FA: deposit (buffer.c:20)
==10846== by 0x40124F: getInputStream (test.c:80)
==10846== by 0x401C55: _st_thread_main (in a.out)
==10846== by 0x401D1D: st_thread_create (in a.out)
我对 Valgrind 很陌生(而且只是 C 的新手),所以我一直在寻找其他有类似 Valgrind 输出的人,但我找不到任何有帮助的东西。
最后的几点说明(抱歉这么长!):这是给学校的,所以如果可能的话,我更喜欢指导而不是代码。此外,虽然 Valgrind 的输出确实提到了“无效读取”和 down 函数之间的联系,但我可以确认 down 函数 - 以及信号量类型本身 - 可以自行正常运行。最后,我在 createBuffer() 中初始化缓冲区和分配内存时确实遇到了一些困难;结合 seg 错误的性质,如果错误与该函数有关,我不会感到惊讶,尽管 Valgrind 没有引用它。
TL;DR:程序中的段错误,作为 Valgrind 的新手并且对 C 相当缺乏经验,我很难理解。上面的代码。谢谢!
编辑:由于下面给出的有用建议而意识到我的错误后,我在尝试为我的信号量分配内存以防止它们丢失时遇到了另一个段错误。我尝试了几种方法,每种方法都具有相同的结果。我的新尝试如下:
buff->mutex = calloc(1,sizeof(semaphore));
buff->mutex = &mutex;
和
buff->mutex = calloc(1,sizeof(semaphore));
createSem(buff->mutex,1);
//I removed the previous semaphore declarations and calls to createSem(...)
与我的第一个问题一样,当信号量首次在 createBuffer(...) 之外引用时,这些仍然会导致分段错误。