Skip to content

Instantly share code, notes, and snippets.

@Aposhian
Created May 17, 2022 19:58
Show Gist options
  • Save Aposhian/0deaad0cbe3ce307e37090f369071dff to your computer and use it in GitHub Desktop.
Save Aposhian/0deaad0cbe3ce307e37090f369071dff to your computer and use it in GitHub Desktop.
double free from shared_from_this
#include <memory>
class Outside;
class Inside {
public:
Inside(std::shared_ptr<Outside> outside): outside_(outside) {}
private:
std::shared_ptr<Outside> outside_;
};
class Outside : public std::enable_shared_from_this<Outside> {
public:
Outside(): inside_(
std::move(
std::make_unique<Inside>(
std::shared_ptr<Outside>(this)
)
)
) {}
void init() {
inside_ = std::make_unique<Inside>(shared_from_this());
}
private:
std::unique_ptr<Inside> inside_;
};
int main() {
auto outside = std::make_shared<Outside>();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment