Cppcheck (version 1.73) does not seem to detect a memory leak in the following code (dealloc.cpp):
class Cat
{
private:
int color;
public:
Cat()
{
color = 1;
}
};
int main()
{
Cat* myCat = new Cat();
return 0;
}
cppcheck --enable=all dealloc.cpp gives:
Checking dealloc.cpp... [dealloc.cpp:16]: (style) Variable 'myCat' is assigned a value that is never used.
In an answer to a question posted on Stack Overflow about one year ago, it was stated that a definite memory leak could not be determined in these types of cases because classes may do their own memory management. It was also stated that there used to be an option to tune this that was no longer available, but may be again someday. Is there a way to detect this leak now, via a .cfg file or otherwise? Any information or guidance would be greatly appreciated - thank you!
Link to Stack Overflow question: http://stackoverflow.com/questions/31400375/cppcheck-does-not-detect-memory-leak
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If the class has a constructor then Cppcheck does not currently warn.
We could make Cppcheck smarter. For instance if the constructor only sets some simple POD variables then we can report the leak. I think it would be possible to do that. Imho to make a real difference - this analysis has to use whole-program-analysis. The class implentation is usually not available in the translation unit currently analysed.
If you want to have a quick fix you could download and compile Cppcheck yourself. It's relatively easy and straightforward imho. If you modify the CheckMemoryLeak::isclass() method so it always returns false then you should get warnings for all classes.
Last edit: Daniel Marjamäki 2016-07-28
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Cppcheck (version 1.73) does not seem to detect a memory leak in the following code (dealloc.cpp):
class Cat
{
private:
int color;
public:
Cat()
{
color = 1;
}
};
int main()
{
Cat* myCat = new Cat();
return 0;
}
cppcheck --enable=all dealloc.cpp gives:
Checking dealloc.cpp...
[dealloc.cpp:16]: (style) Variable 'myCat' is assigned a value that is never used.
In an answer to a question posted on Stack Overflow about one year ago, it was stated that a definite memory leak could not be determined in these types of cases because classes may do their own memory management. It was also stated that there used to be an option to tune this that was no longer available, but may be again someday. Is there a way to detect this leak now, via a .cfg file or otherwise? Any information or guidance would be greatly appreciated - thank you!
Link to Stack Overflow question:
http://stackoverflow.com/questions/31400375/cppcheck-does-not-detect-memory-leak
Cppcheck still does not report about that.
If the class has a constructor then Cppcheck does not currently warn.
We could make Cppcheck smarter. For instance if the constructor only sets some simple POD variables then we can report the leak. I think it would be possible to do that. Imho to make a real difference - this analysis has to use whole-program-analysis. The class implentation is usually not available in the translation unit currently analysed.
If you want to have a quick fix you could download and compile Cppcheck yourself. It's relatively easy and straightforward imho. If you modify the CheckMemoryLeak::isclass() method so it always returns false then you should get warnings for all classes.
Last edit: Daniel Marjamäki 2016-07-28
Thank you! I tried the quick fix you described and it detected the leak in the code example. Will now try on our project.