Advertisement
Guest User

RAII and Stack-unwinding

a guest
Apr 5th, 2011
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. class resource
  7. {
  8. public:
  9.   resource(const string & resource_name_)
  10.     : _resource_name(resource_name_)
  11.   {
  12.     cout << "resource::resource()" << endl;
  13.  
  14.     cout << "acquired resource: "
  15.          << _resource_name << endl;
  16.   }
  17.  
  18.   ~resource()
  19.   {
  20.     cout << "resource::~resource()" << endl;
  21.  
  22.     cout << "releasing resource: "
  23.          << _resource_name << endl;
  24.   }
  25. private:
  26.   string _resource_name;
  27. };
  28.  
  29. class raii_test
  30. {
  31. public:
  32.   raii_test()
  33.     : _rt("resource_1") {};
  34.  
  35.   void f1()
  36.   {
  37.     resource local_resource("resource_2");
  38.  
  39.     cout << "raii_test::f1()" <<endl;
  40.  
  41.     f2();
  42.   }
  43.  
  44.   void f2()
  45.   {
  46.     cout << "raii_test::f2()" << endl;
  47.  
  48.     cout << "Throwing" << endl;
  49.  
  50.     throw runtime_error("Timepass");
  51.   }
  52.  
  53.   void operator()()
  54.   {
  55.     cout << "In raii_test::operator()" << endl;
  56.  
  57.     f1();
  58.   }
  59.  
  60.   ~raii_test()
  61.   {
  62.     cout << "raii_test::~raii_test" << endl;
  63.   }
  64.  
  65. private:
  66.   resource _rt;
  67. };
  68.  
  69. int
  70. main()
  71. {
  72. //   try
  73.     {
  74.       raii_test obj;
  75.  
  76.       obj();
  77.     }
  78. //   catch (...)
  79. //     {
  80. //       throw;
  81. //     }
  82. }
  83.  
  84. -----
  85.  
  86. Uncommenting the commented-out code causes RAII-enabled resource deallocation to work correctly. Here are the outputs with commented and uncommented code that show that:
  87.  
  88. Output with commented-out code:
  89. main()
  90. resource::resource()
  91. acquired resource: resource_1
  92. In raii_test::operator()
  93. raii_test::f1()
  94. resource::resource()
  95. acquired resource: resource_2
  96. raii_test::f2()
  97. Throwing
  98. terminate called after throwing an instance of 'std::runtime_error'
  99.   what():  foo bar baz
  100. Aborted
  101.  
  102. Output without commented code. Note the correct deallocation/release of acquired resources:
  103. main()
  104. resource::resource()
  105. acquired resource: resource_1
  106. In raii_test::operator()
  107. raii_test::f1()
  108. resource::resource()
  109. acquired resource: resource_2
  110. raii_test::f2()
  111. Throwing
  112. resource::~resource()
  113. releasing resource: resource_2
  114. raii_test::~raii_test
  115. resource::~resource()
  116. releasing resource: resource_1
  117. terminate called after throwing an instance of 'std::runtime_error'
  118.   what():  foo bar baz
  119. Aborted
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement