Cppcheck now has a check to detect references to temporary which leads to false positives (danglingTemporaryLifetime
) for code involving VCL classes (of C++Builder6).
Here is test.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <string>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:
TButton *Button1;
public:
explicit __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
class TTest
{
public:
std::string GetText() const;
void Init()
{
m_form->Caption = GetText().c_str();
m_form->Button1->Caption = "Text";
}
TForm1* m_form;
// Forbidden:
TTest(const TTest&);
TTest& operator=(const TTest&);
};
//---------------------------------------------------------------------------
an example that compiles in BCB6 and, if invoked like this
"C:\Program Files\Cppcheck\cppcheck.exe" --enable=style --inconclusive test.cpp
reproduces the error:
Checking test.cpp ...
test.cpp:23:9: error: Using pointer to temporary. [danglingTemporaryLifetime]
m_form->Button1->Caption = "Text";
^
test.cpp:22:34: note: Pointer to container is created here.
m_form->Caption = GetText().c_str();
^
test.cpp:23:9: note: Using pointer to temporary.
m_form->Button1->Caption = "Text";
^
I learned that one should not provide the include path to the standard library and never tried to do it for the VCL either. But now Cppcheck doesn't cope with the Caption
property of a class that's not known. The error is given for the next member access (that to Button1
which is known) and the unknown member with the temporary assigned to it still has an effect here.
I also tried to tell the VCL include path to Cppcheck by adding
-I "C:\Program Files (x86)\Borland\CBuilder6\Include\Vcl"
But this leads to
C:\Program Files (x86)\Borland\CBuilder6\Include\Vcl\sysvari.h:3365:0: error: No pair for character ("). Can't process file. File is either invalid or unicode, which
is currently not supported. [preprocessorErrorDirective]
#pragma message "ERROR: sizeof(TVarData) < sizeof(VARIANT)'
^
How to solve this problem?
Cppcheck has a bit of handling of such class properties. I don't want Cppcheck to write garbage for your code.
Can you show me how to reproduce? I have put your code in a file 1.cpp and with the command
cppcheck --enable=style --inconclusive 1.cpp
I get no warning.