Does the code below produce a memory leak?
#include <utility>
#include <stdexcept>
struct A {};
struct B {
B() {
throw std::runtime_error("");
}
};
template <class T>
class MyPtr {
public:
MyPtr(T* p) : m_p(p) {}
MyPtr(MyPtr&& other) : m_p(other.m_p) {
other.m_p = nullptr;
}
~MyPtr() {
delete m_p;
}
private:
T* m_p;
};
class Foo {
public:
Foo(MyPtr<A> a, MyPtr<B> b) : m_a(std::move(a)), m_b(std::move(b)) {}
private:
MyPtr<A> m_a;
MyPtr<B> m_b;
};
int main() {
try {
Foo foo(new A(), new B());
}
catch (const std::exception&) {
}
return 0;
}
Is there a difference between
Foo foo(new A(), new B());
and
Foo foo(MyPtr(new A()), MyPtr(new B()));
?