Skip to content

Commit 4807713

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

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

08-exceptions/myvec-demo/controllable.hh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +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-
rhs.resource_ = nullptr;
14-
}
13+
Controllable(Controllable &&rhs) noexcept
14+
: resource_(std::move(rhs.resource_)) {}
1515
Controllable &operator=(Controllable &&rhs) noexcept {
1616
std::swap(resource_, rhs.resource_);
1717
return *this;
@@ -30,5 +30,5 @@ struct Controllable {
3030
return *this;
3131
}
3232

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

0 commit comments

Comments
 (0)