I have an object hierarchy and need to be able to clone objects from the base class. I've followed the typical CRTP pattern, except that I also want to be able to return the child class if copy is called on a child directly. To do that, I've followed the suggestion here: https://stackoverflow.com/a/30252692/1180785
It seems to work fine, but Clang warns me that I have a potential memory leak. I've reduced the code down to this MCVE:
template <typename T>
class CRTP {
protected:
virtual CRTP<T> *internal_copy(void) const {
return new T(static_cast<const T&>(*this));
}
public:
T *copy(void) const {
return static_cast<T*>(internal_copy());
}
virtual ~CRTP(void) = default;
};
class Impl : public CRTP<Impl> {
};
int main(void) {
Impl a;
Impl *b = a.copy();
delete b;
}
As far as I can tell, there's no possible memory leak there, but running Clang through XCode shows this:
Is there a memory leak here? If not, what's causing the false positive and how can I work around it? (I'd rather not turn off static analysis)