Archive 17/01/2023.

[SOLVED] SharedPtr in samples

1vanK

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);

}
[/code]
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?

jmiller

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

1vanK

It creates a new SharedPtr from raw pointer

1vanK

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

greenhouse

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

cadaver

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:

http://urho3d.github.io/documentation/HEAD/_conventions.html

1vanK

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

cadaver

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