1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Two objects hold std::shared_ptr to each other. Why is…

Added 30 Nov -0001

Two objects hold std::shared_ptr to each other. Why is neither destructor called when the last external owner releases them?

  1. shared_ptr detects cycles and defers destruction to the next garbage collection
  2. The control blocks are leaked first, so the destructors run but the memory does not
  3. Each object's reference count stays at one because of the mutual strong references Correct answer
  4. The reference counts are merged into a single counter, so they never reach zero

Correct answer

C. Each object's reference count stays at one because of the mutual strong references

14 views

MCQSEXAM

Explanation

Reference counting cannot reclaim cycles: each object is still strongly owned by its partner, so both counts stay at least one and the destructors never run. Break the cycle with std::weak_ptr on one side, or restructure so only one direction is strong. This is the classic shared_ptr leak that memory checkers report as still reachable rather than definitely lost.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs