0

当我尝试解除分配缓冲区时遇到问题。每次尝试将缓冲区传递给我的解除分配方法时,都会发生段错误。Valgrind 确认段错误位于 BufferDeallocate 方法中。

==30960== Memcheck, a memory error detector                                     
==30960== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.       
==30960== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info     
==30960== Command: ./a.out                                                      
==30960==                                                                       
==30960== Conditional jump or move depends on uninitialised value(s)            
==30960==    at 0x40178A6: index (in /lib64/ld-2.15.so)                         
==30960==    by 0x4007642: expand_dynamic_string_token (in /lib64/ld-2.15.so)   
==30960==    by 0x4007EFD: _dl_map_object (in /lib64/ld-2.15.so)                
==30960==    by 0x400135D: map_doit (in /lib64/ld-2.15.so)                      
==30960==    by 0x400E345: _dl_catch_error (in /lib64/ld-2.15.so)               
==30960==    by 0x4001276: do_preload (in /lib64/ld-2.15.so)                    
==30960==    by 0x4003C11: dl_main (in /lib64/ld-2.15.so)                       
==30960==    by 0x40149A7: _dl_sysdep_start (in /lib64/ld-2.15.so)              
==30960==    by 0x40049DC: _dl_start (in /lib64/ld-2.15.so)                     
==30960==    by 0x4000BA7: ??? (in /lib64/ld-2.15.so)                           
==30960==                                                                       
==30960== Conditional jump or move depends on uninitialised value(s)            
==30960==    at 0x40178AB: index (in /lib64/ld-2.15.so)                         
==30960==    by 0x4007642: expand_dynamic_string_token (in /lib64/ld-2.15.so)   
==30960==    by 0x4007EFD: _dl_map_object (in /lib64/ld-2.15.so)                
==30960==    by 0x400135D: map_doit (in /lib64/ld-2.15.so)                      
==30960==    by 0x400E345: _dl_catch_error (in /lib64/ld-2.15.so)               
==30960==    by 0x4001276: do_preload (in /lib64/ld-2.15.so)                    
==30960==    by 0x4003C11: dl_main (in /lib64/ld-2.15.so)                       
==30960==    by 0x40149A7: _dl_sysdep_start (in /lib64/ld-2.15.so)              
==30960==    by 0x40049DC: _dl_start (in /lib64/ld-2.15.so)                     
==30960==    by 0x4000BA7: ??? (in /lib64/ld-2.15.so)                           
==30960==                                                                       
==30960== Thread 2:                                                             
==30960== Use of uninitialised value of size 8                                  
==30960==    at 0x400C97: bufferRead (in /home/cward/cs370/Project04/a.out)     
==30960==    by 0x4E39D95: start_thread (in /lib64/libpthread-2.15.so)          
==30960==                                                                       
==30960== Thread 1:                                                             
==30960== Invalid read of size 4                                                
==30960==    at 0x400919F: do_lookup_x (in /lib64/ld-2.15.so)                   
==30960==    by 0x4009B31: _dl_lookup_symbol_x (in /lib64/ld-2.15.so)           
==30960==    by 0x400D880: _dl_fixup (in /lib64/ld-2.15.so)                     
==30960==    by 0x4014154: _dl_runtime_resolve (in /lib64/ld-2.15.so)           
==30960==    by 0x400AAA: main (in /home/cward/cs370/Project04/a.out)           
==30960==  Address 0x6f57206f6c6c6550 is not stack'd, malloc'd or (recently) free'd
==30960==                                                                       
==30960==                                                                       
==30960== Process terminating with default action of signal 11 (SIGSEGV)        
==30960==  General Protection Fault                                             
==30960==    at 0x400919F: do_lookup_x (in /lib64/ld-2.15.so)                   
==30960==    by 0x4009B31: _dl_lookup_symbol_x (in /lib64/ld-2.15.so)           
==30960==    by 0x400D880: _dl_fixup (in /lib64/ld-2.15.so)                     
==30960==    by 0x4014154: _dl_runtime_resolve (in /lib64/ld-2.15.so)           
==30960==    by 0x400AAA: main (in /home/cward/cs370/Project04/a.out)           
[boundedbuffer] Added: Hello World!                                             

[boundedbuffer] in bRead                                                        
[main] Printing from out_array:                                                 
Hello World!                                                                    

==30960==                                                                       
==30960== HEAP SUMMARY: 
==30960==     in use at exit: 1,872 bytes in 2 blocks                           
==30960==   total heap usage: 3 allocs, 1 frees, 2,144 bytes allocated          
==30960==               

我将提供所有文件,以便每个人都能看到代码方面的情况。

这是我的 BoundedBuffer.c 文件:

<snip>

有界缓冲区.h

<snip>

这是我们的主文件,用于测试 BoundedBuffer。

主要.c:

<snip>  

任何有关解决此问题的建议都会很棒!我对 C 编码很陌生,而释放/分配是 C 中让我困惑的主题之一。

4

1 回答 1

1

在您的 Main.c 中,readParams.b = writeParams.b;当您尝试解除分配时,您设置它会导致错误。C 的free方法不像 C++ 的删除,事实上,从这里开始,因为你试图释放相同的数据两次它会导致未定义的行为

换句话说,由于读取和写入参数缓冲区都指向同一个对象,因此您应该只对其中一个对象进行 bufferDeallocate,因为一旦内存被释放,您就不再可以控制它并尝试释放您没有的内存控制是疯狂的。

于 2013-04-30T20:29:54.220 回答