Menu

False positive: error : Using object that is a temporary.

2024-10-09
2024-10-20
  • Jean Porcherot

    Jean Porcherot - 2024-10-09

    This piece of code generates "error : Using object that is a temporary.".

    #include <string>
    #include <vector>
    #include <sstream>
    
    inline void MyStrCp( char* Dst, size_t DstSize, const char* Src )
    {
    #ifdef WIN32
        strcpy_s(Dst, DstSize, Src);
    #else
        strcpy(Dst, Src);
    #endif
    }
    
    class MyChannelInfo
    {
    public:
        MyChannelInfo()
        {
        }
        ~MyChannelInfo()
        {
            delete m_name;
        }
    
        MyChannelInfo( const char* name )
        {
            m_name = new char[strlen(name)+1];
            MyStrCp( m_name, strlen(name)+1, name );
        }
    
        MyChannelInfo& operator=( const MyChannelInfo& info )
        {
            if ( this != &info )
            {
                delete m_name;
                m_name = new char[strlen(info.m_name)+1];
                MyStrCp( m_name, strlen(info.m_name)+1, info.m_name );
            }
    
            return *this;
        }
        MyChannelInfo( const MyChannelInfo& info )
        {
            operator=( info );
        }
    
        inline const char* GetName() const { return m_name; }
    
        char* m_name = NULL;
    };
    
    typedef std::vector<MyChannelInfo> MyChannelInfos;
    
    class MyChannel
    {
    public:
        MyChannel()
        {
        }
    
        std::string GetComment()
        {
            std::stringstream str;
            str << "My comment";
            return str.str();
        }
    };
    
    class FillVector
    {
        MyChannelInfos& channels;
    
    public:
    
        FillVector( MyChannelInfos& _channels ) : 
            channels( _channels )
        {}
        virtual void Visit1( MyChannel& channel ) 
        { 
            channels.push_back( MyChannelInfo( channel.GetComment().c_str() ) );
        }
    
        virtual void Visit2()
        {
            channels.push_back( MyChannelInfo( "foo" ) ); // reports error : Using object that is a temporary.
        }
    };
    

    The tool seems lost, because it reports this error in Visit2, while it looks like Visit1 is actually causing problem: If I replace MyChannel::GetComment by:

    const std::string& GetComment()
        {
            static std::string comment = "";
            return comment;
        }
    

    Then the problem disappear...

    However, retruning a std::string by copy in GetComment is perfectly OK (In my original code, the GetComment returns a string computed from other attributes of the class, no way to retrun a const reference). The string remains valid while MyChannelInfo constructor is called and char* copied to MyChannelInfo::m_name.

    There should be no error here.

     
  • Oleksandr Labetskyi

    Greetings Jean. Would you be so kind to provide information about which cppcheck version was used with this example?

     
  • Daniel Marjamäki

    Thanks! I have created this ticket: https://trac.cppcheck.net/ticket/13244

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.