Skip to content

Commit 8bbe4a3

Browse files
committed
Fix unexpected memory leak for custom vector example
1 parent 731d3db commit 8bbe4a3

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

08-exceptions/myvec-demo/controllable.hh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#pragma once
22

33
#include <iostream>
4+
#include <memory>
45
#include <stdexcept>
56
#include <utility>
67

7-
struct Controllable {
8+
struct Controllable final {
89
static int control;
9-
int *resource_;
10+
std::unique_ptr<int> resource_;
1011
Controllable() : resource_(new int(42)) {}
1112

12-
Controllable(Controllable &&rhs) noexcept : resource_(rhs.resource_) {
13+
Controllable(Controllable &&rhs) noexcept
14+
: resource_(std::move(rhs.resource_)) {
1315
rhs.resource_ = nullptr;
1416
}
1517
Controllable &operator=(Controllable &&rhs) noexcept {
@@ -30,5 +32,5 @@ struct Controllable {
3032
return *this;
3133
}
3234

33-
~Controllable() { delete resource_; }
35+
~Controllable() = default;
3436
};

0 commit comments

Comments
 (0)