[SOLVED] SharedPtr in samples

I do not understand

[code]void AnimatingScene::SetupViewport()
Renderer* renderer = GetSubsystem();

// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
renderer->SetViewport(0, viewport);

SharedPtr viewport is local variable
SharedPtr viewport convert to raw pointer Viewport* (reference count not increased) and used in SetViewport()
SharedPtr viewport deleted as local variable (reference count == 0)
Why Viewport* is not destroyed?

Renderer::SetViewport() also stores the pointer in Renderer’s Vector<SharedPtr > viewports_

It creates a new SharedPtr from raw pointer

Oh, I understand my mistake. Ref coundet stored in object, but not in SharedPtr

I thought the same…Then what is the functionality behind SharedPtr? To update Ref Counted state of a pointed to object?

Yes, it automatizes strong refcount management, so that you don’t have to do manual calls to increment / decrement it.

Intrusive shared pointers may be strange at first to someone coming from a std::shared_ptr background, but once you understand how they work, there should be relatively few ways to shoot yourself in the foot with them. The tendency of API functions to store shared pointers internally is documented in the conventions:


I think here we need another example. Drawable has no the member SetMaterial. Only some derived classes

Thanks for bringing to attention. Now StaticModel & Renderer are used as examples.