tdauth / cpp-futures-promises

Advanced futures and promises in C++.
GNU General Public License v3.0
16 stars 0 forks source link

Memory error #19

Closed tdauth closed 5 years ago

tdauth commented 5 years ago

There is a destructor call of a promise with an empty state in Future<T>::then<S>() when using onComplete. Actually, there should be only one destructor call of a promise (the copy) and not two. Where does the promise with the empty state come from? How is it possible to create a promise with an empty state?

Memory error can be reproduced:

auto p = createPromiseInt();
p.trySuccess(10);
auto f = p.future();
auto tmp = createPromiseInt();
f.onComplete([tmp](const Try<int> &t) {});

Without capturing tmp, everything works fine.

The fix is actually very simple: Remove move constructors and assignment operators from Promise and Future. Otherwise, the shared pointer of Promise can become invalid. We do not need to move these types. They are designed to be copied.