I think what is wrong is that there is one more call to “forget” than “remember”.
If you change your unit test this way:
TEST(setModalView,
auto frame = owned (new CFrame (CRect (0, 0, 100, 100), nullptr));
auto view = shared (new View ());
auto nbReferences = view->getNbReference (); // here nbReference == 2
EXPECT (frame->getModalView () == nullptr);
auto session = frame->beginModalViewSession (view);
EXPECT (session);
EXPECT (frame->getModalView () == view);
auto container = shared (new CViewContainer (CRect (0, 0, 0, 0)));
auto session2 = frame->beginModalViewSession (container);
EXPECT (session2)
EXPECT (frame->getModalView () == container);
EXPECT (frame->endModalViewSession (*session) == false);
EXPECT (frame->endModalViewSession (*session2) == true);
EXPECT (frame->getModalView () == view);
EXPECT (frame->endModalViewSession (*session) == true);
EXPECT (frame->getModalView () == nullptr);
EXPECT (view->getNbReference () == nbReferences); // here nbReferences == 1 => will fail
);
Then this test will fail.
I used “owned” in my code when you are using “shared” (for the view) because in my mind I own the view. I give it to the modal view session API so that you can use it. But this API is doing more “forget” than “remember” and I don’t think that is a good thing. It should be neutral.
It’s fine to leave it the way you have it but then I think you should change the documentation to state that it is taking ownership of the view provided.