#include <iostream>
#include <memory>
using namespace std;
struct Connection {
Connection( ) {
cout << "connection open" << endl;
}
~Connection( ) {
cout << "connection closed" << endl;
}
} ;
struct Reader {
Reader( shared_ptr< Connection> c) :
c( c) {
cout << "Reader created" << endl;
}
~Reader( ) {
cout << "Reader deleted" << endl;
}
shared_ptr< Connection> c;
} ;
struct Holder {
shared_ptr< Connection> c;
Reader* r;
Holder( ) :
c( make_unique< Connection> ( ) ) {
cout << "Holder created" << endl;
r = new Reader( c) ;
}
~Holder( ) {
if ( r) { delete r; }
cout << "Holder deleted" << endl;
}
} ;
int main( ) {
{
Holder h;
}
{
Holder h;
}
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgoKc3RydWN0IENvbm5lY3Rpb24gewoJQ29ubmVjdGlvbigpIHsKCQljb3V0IDw8ICJjb25uZWN0aW9uIG9wZW4iIDw8IGVuZGw7Cgl9Cgl+Q29ubmVjdGlvbigpIHsKCQljb3V0IDw8ICJjb25uZWN0aW9uIGNsb3NlZCIgPDwgZW5kbDsKCX0KfTsKCgpzdHJ1Y3QgUmVhZGVyIHsKCVJlYWRlcihzaGFyZWRfcHRyPENvbm5lY3Rpb24+IGMpIDogCgljKGMpIHsKCQljb3V0IDw8ICJSZWFkZXIgY3JlYXRlZCIgIDw8IGVuZGw7Cgl9Cgl+UmVhZGVyKCkgeyAKCQljb3V0IDw8ICJSZWFkZXIgZGVsZXRlZCIgIDw8IGVuZGw7Cgl9CglzaGFyZWRfcHRyPENvbm5lY3Rpb24+IGM7Cn07CgoKc3RydWN0IEhvbGRlciB7CglzaGFyZWRfcHRyPENvbm5lY3Rpb24+IGM7CglSZWFkZXIqIHI7CglIb2xkZXIoKSA6CgljKG1ha2VfdW5pcXVlPENvbm5lY3Rpb24+KCkpIHsKCQljb3V0IDw8ICJIb2xkZXIgY3JlYXRlZCIgIDw8IGVuZGw7CgkJciA9IG5ldyBSZWFkZXIoYyk7Cgl9Cgl+SG9sZGVyKCkgeyAKCQlpZiAocikgeyBkZWxldGUgcjsgfQoJCWNvdXQgPDwgIkhvbGRlciBkZWxldGVkIiAgPDwgZW5kbDsKCX0KfTsKCgppbnQgbWFpbigpIHsKCXsKCQlIb2xkZXIgaDsKCX0KCQoJCgl7CgkJSG9sZGVyIGg7Cgl9CgkKICAgIHJldHVybiAwOwp9