簡體   English   中英

拋出異常時valgrind中的錯誤

[英]error in valgrind while throwing exceptions

大家好,我正在嘗試完成涉及我自己的異常的代碼,但是在運行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: _IO_file_xsputn@@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: _IO_file_xsputn@@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== 
 return (s.str().c_str());

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

基本上是相同的問題:

return (k.c_str());

將指針返回到本地字符串k的緩沖區中,該指針將立即超出范圍。

為了解決這個問題,只需刪除該指針業務,然后按值返回一個std::string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM