0

バッファの割り当てを解除しようとすると問題が発生します。バッファを deallocate メソッドに渡そうとするたびに、segfault が発生します。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 のコーディングにかなり慣れていないので、解放/割り当ては、私を困惑させる C のトピックの 1 つです。

4

1 に答える 1

1

In your Main.c, you set readParams.b = writeParams.b; this causes an error when you try to deallocate. C's free method is not like C++ delete, in fact, from here, as you are trying to free the same data twice it results in undefined behaviour.

In other words, as both the read and write params buffers point to the same object, you should only bufferDeallocate one of them, as once the memory has been freed, you no longer have control over it and attempting to free memory you don't control is madness.

于 2013-04-30T20:29:54.220 に答える