- $ cat sharedptrloop.c
- #include <iostream>
- #include <memory>
- #include <mutex>
- #include <thread>
- class myobj
- {
- public:
- std::shared_ptr<myobj> ref_p = NULL;
- myobj() {
- std::cout << "myobj begin" << std::endl;
- }
- ~myobj() {
- std::cout << "myobj DIE" << std::endl;
- }
- };
- int main(int ac, char *av[])
- {
- std::shared_ptr<myobj> p = std::make_shared<myobj>();
- p->ref_p = p;
- // without |reset()| the destructor of |p| will never be called
- // p->ref_p.reset();
- return EXIT_SUCCESS;
- }
sharedptrloop.c shared_ptr reference loop demo 1
Posted by Anonymous on Thu 21st Nov 2024 09:57
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.