2012-01-18 104 views
1

我正在運行一個應用程序,由於分段錯誤而停止在一個點上。 我將嘗試登記環境: - 應用程序有一個包含std :: string成員(數據)的類(Generator),並且該成員已正確初始化爲「HelloWorld」。 - 此對象的指針傳遞給另一個類(Product)的成員函數(send1)。只要我嘗試在函數內部打印數據的值,就會導致分段錯誤。如果我在調用send1函數之前嘗試打印數據的值,它會正確打印。std :: string上的分段錯誤

下面是GDB輸出:

(gdb) br Generator::test 
Breakpoint 1 at 0x80499ef: file ../app/generator/src/generator.cpp, line 58. 
(gdb) br Product::send1 
Breakpoint 2 at 0x804a17e: file ../app/configurator/src/product.cpp, line 43. 
(gdb) run 
[Thread debugging using libthread_db enabled] 
[New Thread -1208071520 (LWP 18389)] 
[Switching to Thread -1208071520 (LWP 18389)] 

Breakpoint 1, Generator::test (this=0x9917020) at ../app/generator/src/generator.cpp:58 
58     cout << "data = " << this->data << endl; 
(gdb) n 
data = HelloWorld 
59     Product* ptr = new Product; 
(gdb) n 
60     bool status = ptr->send1(this); 
(gdb) s 

Breakpoint 2, Product::send1 (this=0x99170c8, genptr=0x9917020) at ../app/configurator/src/product.cpp:43 
43    cout << genptr->data << endl; 
(gdb) p genptr->data 
$1 = {static npos = 4294967295, 
    _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
    _M_p = 0x99170b4 "HelloWorld"}} 
(gdb) n 

Program received signal SIGSEGV, Segmentation fault. 
0x076751e6 in std::operator<< <char, std::char_traits<char>, std::allocator<char> >() from /usr/lib/libstdc++.so.6 
(gdb) bt 
#0 0x076751e6 in std::operator<< <char, std::char_traits<char>, std::allocator<char> >() from /usr/lib/libstdc++.so.6 
#1 0x0804a19a in Product::send1 (this=0x99170c8, genptr=0x9917020) at ../app/configurator/src/product.cpp:43 
#2 0x08049a85 in Generator::test (this=0x9917020) at ../app/generator/src/generator.cpp:60 
#3 0x08048f4c in Configure::init (this=0x9917008) at ../app/configurator/src/configurator.cpp:89 
#4 0x08048c93 in main (argc=1, argv=0xbfed7364) at ../launch/main/src/appLaunch.cpp:20 
(gdb) 

這裏是Valgrind的輸出

valgrind --tool=memcheck --leak-check=yes ./application 
==18328== Memcheck, a memory error detector. 
==18328== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al. 
==18328== Using LibVEX rev 1575, a library for dynamic binary translation. 
==18328== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP. 
==18328== Using valgrind-3.1.1, a dynamic binary instrumentation framework. 
==18328== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al. 
==18328== For more details, rerun with: -v 
==18328== 
data = HelloWorld 
==18328== Invalid read of size 4 
==18328== at 0x76751E6: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libstdc++.so.6.0.3) 
==18328== by 0x804A199: Product::send1(Generator*) (product.cpp:43) 
==18328== by 0x8049A84: Generator::test() (generator.cpp:60) 
==18328== by 0x8048F4B: Configure::init() (configurator.cpp:89) 
==18328== by 0x8048C92: main (appLaunch.cpp:20) 
==18328== Address 0x5C040234 is not stack'd, malloc'd or (recently) free'd 
==18328== 
==18328== Process terminating with default action of signal 11 (SIGSEGV) 
==18328== Access not within mapped region at address 0x5C040234 
==18328== at 0x76751E6: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/libstdc++.so.6.0.3) 
==18328== by 0x804A199: Product::send1(Generator*) (product.cpp:43) 
==18328== by 0x8049A84: Generator::test() (generator.cpp:60) 
==18328== by 0x8048F4B: Configure::init() (configurator.cpp:89) 
==18328== by 0x8048C92: main (appLaunch.cpp:20) 
==18328== 
==18328== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 1) 
==18328== malloc/free: in use at exit: 159 bytes in 5 blocks. 
==18328== malloc/free: 5 allocs, 0 frees, 159 bytes allocated. 
==18328== For counts of detected errors, rerun with: -v 
==18328== searching for pointers to 5 not-freed blocks. 
==18328== checked 116,636 bytes. 

這實際上是一個更大的應用程序的一部分,但我已經剝離了應用,並使其非常小,所以我可以很容易地調試這個問題,但即使現在我也不知道爲什麼會出現這個問題。使用gdb我試圖檢查內存佈局,但在調用函數之前和之後內存地址和內容看起來完好無損。事實上,如果我試圖使用gdb打印功能打印數據的值,它會出現是正確的。我已經嘗試了很多事情,比如在堆上分配內存等,但似乎沒有任何工作。請求您親切指導我如何開始調試此問題。

+3

很高興知道您設法取消了應用程序,但您忘記了包含源代碼。 – 2012-01-18 18:37:37

+0

- 一些代碼將非常有幫助 - 這些發電機和產品在同一個項目中嗎?也許其中一個是在一個動態庫? – Alek86 2012-01-18 19:00:21

+0

是的發電機和產品都是同一個項目的一部分。我認爲valgrind指向一個無效的讀取,但我不知道如何解釋和調試它。 – vchandra 2012-01-18 19:24:31

回答

0

我最好的猜測是genptr已被刪除/銷燬,但它所使用的內存在調試器中打印出genptr-> data的內容時尚未被覆蓋。然而,ostream operator<<函數在內部分配了一些內存,並且恰好重複使用了相同的空間,所以它被實際上的時間覆蓋並嘗試打印字符串,導致崩潰。

您可以通過查看SEGV後0x9917020(其中genptr指向的內存)的內存來查看它是否已更改。你應該能夠在上面的最後gdb提示符下執行up,然後執行p genptr->data

+0

嗨克里斯,我嘗試了你的建議,但內存並沒有改變 – vchandra 2012-01-18 22:05:11

+0

嗨,克里斯,如果我做'上'在gdb和打印數據,它會再次打印。順便說一句,你有什麼想法'地址0x5C040234不堆棧,malloc'd或(最近)free'd'表示?這在valgrind輸出中顯示。我可以設置一些檢查點或任何可以追蹤此問題的方法嗎?我只是不知道從哪裏開始。 – vchandra 2012-01-18 22:07:45

+0

我進一步剝離了代碼。現在有一個小應用程序以及兩個不同的靜態庫。有第三個靜態庫,我沒有鏈接,但我發現,只要我從這個庫中包含頭文件,就會導致分段錯誤。僅僅包含一個頭文件可能會導致seg故障嗎? – vchandra 2012-01-25 15:27:25

1

很高興看到send1的聲明和實現,否則很難提供幫助。產品/發電機有任何虛擬功能嗎?

爲什麼寫COUT < < 「數據=」 < <這 - >數據< < ENDL;而不僅僅是數據?這有什麼區別嗎?

嘗試更改Product :: send1(Generator * genptr)到Product :: send1(const Generator & gen),並用ptr-> send1(* this)調用它,看看是否有同樣的問題。