释放/释放缓冲区时的Segfault

时间:2013-04-30 20:25:32

标签: c segmentation-fault free memory-management

当我尝试释放缓冲区时,我遇到了问题。每次尝试将缓冲区传递给我的deallocate方法时,都会发生段错误。 Valgrind确认segfault位于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>

BoundedBuffer.h

<snip>

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

MAIN.C:

<snip>  

任何有关解决此问题的建议都会令人惊叹!我对C编码很陌生,Freeing / Allocing是C中令我困惑的主题之一。

1 个答案:

答案 0 :(得分:1)

在您的Main.c中,设置readParams.b = writeParams.b;这会在您尝试取消分配时导致错误。 C free方法实际上与here中的C ++删除不同,因为您尝试将相同的数据释放两次会导致未定义的行为

换句话说,因为read和write params缓冲区都指向同一个对象,所以你应该只缓冲它们中的一个,因为一旦释放了内存,你就不再能控制它并尝试释放内存了不要控制是疯了。