2016-01-21 177 views
1

嗨,大家好,即時嘗試完成一個代碼,涉及我自己的異常,但即時通訊運行valgrind時有一些錯誤。 錯誤不會一直髮生,通常只有第一次嘗試。它看起來好像是異常的字符串給出了麻煩,但它仍然會在拋出異常時打印它......任何想法可能會導致這些問題? THX您的時間:)Valgrind錯誤同時拋出異常

 #include <iostream> 
     #include <sstream> 
     #include <exception> 
     using namespace std; 
     class wrongMessageIndex:public exception{ 
public: 
    int index; 
public: 
    wrongMessageIndex(int& num){index=num;}; 
    virtual const char* what() const throw(){ 
     stringstream s; 
     string k="Wrong message index: "; 
     s<<k<<index; 
     return (s.str().c_str()); 
    } 

}; 
class NoUserInSystem:public exception{ 
public: 
    string user; 
public: 
    NoUserInSystem(string name){user=name;}; 
    virtual const char* what() const throw(){ 
     string k=user; 
       k+=": no such user "; 
     return (k.c_str()); 
    } 
    virtual ~NoUserInSystem() throw(){} 
}; 
class MemoreyFail:public exception{ 
public: 
    virtual const char* what() const throw(){ 
     return ("Unable to create new message or write in the right file"); 
    } 
    virtual ~MemoreyFail() throw(){} 
}; 

從中拋出異常

string user=""; 
int checker=0; 
while(!file.eof()){ 
    getline(file,user); 
    if(strcmp(user.c_str(),to_who.c_str())==0) 
    { 
     checker=-1; 
    } 
} 
if(checker!=-1||strcmp(this->nameOfUser.c_str(),to_who.c_str())==0){ 
throw NoUserInSystem(to_who); 
} 

這就是異常被捕獲的部分:

try{ 
      this->sendSingleMessage(); 
     } 
     catch(exception& e){ 
      cout<<e.what()<<endl; 
     } 

這裏幾個我收到的錯誤

Welcome zamri , you have 2 new messages. 
Eva would like to update you that: 
What would you like to do? 
(1) See the list of all messages 
(2) Read the next new message 
(3) Read a message by index number 
(4) Read a complete correspondence 
(5) Write a new message 
(6) Write a new mass-message 
(7) Quit 
Please type you choice 
5 
To:tony 
==2945== Invalid read of size 1 
==2945== at 0x402C658: strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40CAC2D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== Address 0x4342834 is 12 bytes inside a block of size 32 free'd 
==2945== at 0x402ACFC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40D699A: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804D2A2: RegularUser::showMenue() (in /home/ise/Ass4/myMs) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== 
==2945== Invalid read of size 1 
==2945== at 0x402C663: strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40CAC2D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== Address 0x4342835 is 13 bytes inside a block of size 32 free'd 
==2945== at 0x402ACFC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40D699A: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804D2A2: RegularUser::showMenue() (in /home/ise/Ass4/myMs) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== 
==2945== Invalid read of size 1 
==2945== at 0x41B868B: [email protected]@GLIBC_2.1 (fileops.c:1330) 
==2945== by 0x41AD757: fwrite (iofwrite.c:45) 
==2945== by 0x40C9075: __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::xsputn(char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x40CA9AA: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x40CAC3D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== Address 0x4342846 is 30 bytes inside a block of size 32 free'd 
==2945== at 0x402ACFC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40D699A: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804D2A2: RegularUser::showMenue() (in /home/ise/Ass4/myMs) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== 
==2945== Invalid read of size 1 
==2945== at 0x41B869F: [email protected]@GLIBC_2.1 (fileops.c:1330) 
==2945== by 0x41AD757: fwrite (iofwrite.c:45) 
==2945== by 0x40C9075: __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::xsputn(char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x40CA9AA: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x40CAC3D: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== Address 0x4342845 is 29 bytes inside a block of size 32 free'd 
==2945== at 0x402ACFC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) 
==2945== by 0x40D699A: std::string::_Rep::_M_destroy(std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16) 
==2945== by 0x804D2A2: RegularUser::showMenue() (in /home/ise/Ass4/myMs) 
==2945== by 0x804A144: main (in /home/ise/Ass4/myMs) 
==2945== 

回答

2
return (s.str().c_str()); 

返回指向流對象的緩衝區的臨時副本的內部緩衝區的第一個元素的指針。因此,只要what()完成,此指針就會懸空。

基本上同樣的問題:

return (k.c_str()); 

返回一個指針到本地串k將立即走出去的範圍的緩衝區。

要解決這個問題,只需放下這個指針業務,並簡單地按值返回std::string

+0

我得到錯誤時拋出NoUserInSystem, – Adiz

+0

@Adiz完全相同的問題,'返回(k.c_str());'返回一個指針到本地字符串的緩衝區。 –