I read that smart pointers helpful in situation when constructor generate some exceptions.
The problem is that constructor got some resource before exceptions generation but destructor is not called (and resources permanent busy).
But i can't undestand it properly. My code:
#include <memory>
#include <iostream>
class resOwner {
public:
resOwner() {
std::cout << "Map some huge resources\n";
throw "hi";
}
~resOwner() {
std::cout << "Free some huge resources\n";
}
};
class normal : resOwner {
};
int main (){
try {
std::shared_ptr<resOwner> k (new resOwner());
} catch (...) {}
}
Output is Map some huge resources
.
How to solve this resource leak with smart pointers?