pastebin - collaborative debugging tool
rovema.kpaste.net RSS


sharedptrloop.c shared_ptr reference loop demo 1
Posted by Anonymous on Thu 21st Nov 2024 09:57
raw | new post

  1. $ cat sharedptrloop.c
  2. #include <iostream>
  3. #include <memory>
  4. #include <mutex>
  5. #include <thread>
  6.  
  7. class myobj
  8. {
  9. public:
  10.         std::shared_ptr<myobj> ref_p = NULL;
  11.         myobj() {
  12.                 std::cout << "myobj begin" << std::endl;
  13.         }
  14.         ~myobj() {
  15.                 std::cout << "myobj DIE" << std::endl;
  16.         }
  17. };
  18.  
  19. int main(int ac, char *av[])
  20. {
  21.         std::shared_ptr<myobj> p = std::make_shared<myobj>();
  22.  
  23.         p->ref_p = p;
  24.  
  25. // without |reset()| the destructor of |p| will never be called
  26. //      p->ref_p.reset();
  27.  
  28.         return EXIT_SUCCESS;
  29. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at