I think the analyzer gets confused with static_cast
in your copy
method. If you just change it to dynamic_cast
, it will no longer mark the line as a memory leak. This makes me think that he believes that you might put an instance of the base type ( CRTP<T>
) on its derived type ( T
), which, of course, will be invalid when dereferencing. Obviously, you are not doing this, so it could be a bug in the leak detector, or it could be something more subtle than what I'm thinking about. This may not be a mistake, but if Impl
becomes more complex (e.g. with multiple inheritance), it can become a problem (like your static_cast
in the copy-CTOR call).
This implementation (with fewer throws) also had zero leaks:
template <typename T> class CRTP { protected: virtual T *internal_copy() const { return new T(static_cast<const T&>(*this)); } public: T *copy() const { return internal_copy(); } virtual ~CRTP() = default; };
source share