When to use TSharedPtr over normal object initialization

Melvin Brink
Tue, 21 Feb 2023 13:15:00 +0000
C++

So i am working with the Online-Subsystem.
We need to create an object of the class ‘FOnlineSessionSettings’ which will be used for the session settings. The way this object is initialised is by creating a TSharedPtr.

TSharedPtr SessionSettings = MakeShareable(new FOnlineSessionSettings());

Why does it need to be a shared-pointer? Why can’t it be like this:

FOnlineSessionSettings SessionSettings = FOnlineSessionSettings();

The shared-pointer will clean itself up from memory if i understand it correctly. But a normal object will too if i go out of scope. So what is the difference between the two in this specific usecase?

4 posts - 4 participants