C++ 抛出异常时valgrind出错

C++ 抛出异常时valgrind出错,c++,exception,valgrind,C++,Exception,Valgrind,大家好,我正在尝试完成一个包含我自己的异常的代码,但在运行valgrind时出现了一些错误。 错误并非总是发生,通常只是第一次尝试。似乎异常字符串正在产生问题,但当抛出异常时,它仍会打印该字符串…知道是什么导致了问题吗? 感谢您的时间:) #include <iostream> #include <sstream> #include <exception> using namespace std;

大家好,我正在尝试完成一个包含我自己的异常的代码,但在运行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(){}
};
这是捕获异常的地方:

try{
            this->sendSingleMessage();
        }
        catch(exception& e){
            cout<<e.what()<<endl;
        }
试试看{
此->sendSingleMessage();
}
捕获(例外和e){
库特
返回指向流对象缓冲区临时副本的内部缓冲区的第一个元素的指针。因此,只要
what()
完成,该指针就会悬空

基本上相同的问题:

return (k.c_str());
将指针返回到本地字符串
k
的缓冲区中,该字符串将立即超出范围


要解决这个问题,只需放弃这个指针业务,只需按值返回一个
std::string

@Adiz完全相同的问题,
return(k.c_str());
将指针返回到本地字符串的缓冲区中。
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());
return (k.c_str());