c++ - Segmentation Fault on std::string -
c++ - Segmentation Fault on std::string -
i running application stops @ 1 point because of segmentation fault. seek enlist enviroment: - application has class (generator) contains std::string fellow member (data ) , fellow member duly intialized "helloworld". - pointer of object passed fellow member function(send1) of class (product). seek print value of info within function gives segmentation fault. if seek print value of info before calling send1 function gets correctly printed.
here gdb output :
(gdb) br generator::test breakpoint 1 @ 0x80499ef: file ../app/generator/src/generator.cpp, line 58. (gdb) br product::send1 breakpoint 2 @ 0x804a17e: file ../app/configurator/src/product.cpp, line 43. (gdb) run [thread debugging using libthread_db enabled] [new thread -1208071520 (lwp 18389)] [switching thread -1208071520 (lwp 18389)] breakpoint 1, generator::test (this=0x9917020) @ ../app/generator/src/generator.cpp:58 58 cout << "data = " << this->data << endl; (gdb) n info = helloworld 59 product* ptr = new product; (gdb) n 60 bool status = ptr->send1( ); (gdb) s breakpoint 2, product::send1 (this=0x99170c8, genptr=0x9917020) @ ../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 info fields>}, <no info fields>}, _m_p = 0x99170b4 "helloworld"}} (gdb) n programme received signal sigsegv, segmentation fault. 0x076751e6 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () /usr/lib/libstdc++.so.6 (gdb) bt #0 0x076751e6 in std::operator<< <char, std::char_traits<char>, std::allocator<char> > () /usr/lib/libstdc++.so.6 #1 0x0804a19a in product::send1 (this=0x99170c8, genptr=0x9917020) @ ../app/configurator/src/product.cpp:43 #2 0x08049a85 in generator::test (this=0x9917020) @ ../app/generator/src/generator.cpp:60 #3 0x08048f4c in configure::init (this=0x9917008) @ ../app/configurator/src/configurator.cpp:89 #4 0x08048c93 in main (argc=1, argv=0xbfed7364) @ ../launch/main/src/applaunch.cpp:20 (gdb)
here valgrind output
valgrind --tool=memcheck --leak-check=yes ./application ==18328== memcheck, memory error detector. ==18328== copyright (c) 2002-2005, , gnu gpl'd, julian seward et al. ==18328== using libvex rev 1575, library dynamic binary translation. ==18328== copyright (c) 2004-2005, , gnu gpl'd, openworks llp. ==18328== using valgrind-3.1.1, dynamic binary instrumentation framework. ==18328== copyright (c) 2000-2005, , gnu gpl'd, julian seward et al. ==18328== more details, rerun with: -v ==18328== info = helloworld ==18328== invalid read of size 4 ==18328== @ 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== 0x804a199: product::send1(generator*) (product.cpp:43) ==18328== 0x8049a84: generator::test() (generator.cpp:60) ==18328== 0x8048f4b: configure::init() (configurator.cpp:89) ==18328== 0x8048c92: main (applaunch.cpp:20) ==18328== address 0x5c040234 not stack'd, malloc'd or (recently) free'd ==18328== ==18328== process terminating default action of signal 11 (sigsegv) ==18328== access not within mapped part @ address 0x5c040234 ==18328== @ 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== 0x804a199: product::send1(generator*) (product.cpp:43) ==18328== 0x8049a84: generator::test() (generator.cpp:60) ==18328== 0x8048f4b: configure::init() (configurator.cpp:89) ==18328== 0x8048c92: main (applaunch.cpp:20) ==18328== ==18328== error summary: 1 errors 1 contexts (suppressed: 17 1) ==18328== malloc/free: in utilize @ exit: 159 bytes in 5 blocks. ==18328== malloc/free: 5 allocs, 0 frees, 159 bytes allocated. ==18328== counts of detected errors, rerun with: -v ==18328== searching pointers 5 not-freed blocks. ==18328== checked 116,636 bytes.
this part of bigger application have stripped application , made little can debug issue having no clue why issue occuring. using gdb have tried check memory layout before , after calling function memory addresses , content looks intact. infact if seek print value of info using gdb print function comes out correct. have tried many things allocating memory on heap etc nil seems work. request kindly guide me how can start debugging issue.
it nice see declaration , implementation of send1, hard help otherwise. product/generator have virtual functions?
why write cout << "data= " << this->data << endl; rather data? create difference?
try changing product::send1(generator* genptr) product::send1(const generator& gen) , phone call ptr->send1(*this) , see if has same problem.
c++ segmentation-fault
Comments
Post a Comment