Skip to content

Commit 96185ee

Browse files
properly cleanup pointers on destrcution
1 parent f7a025f commit 96185ee

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎include/livekit/room.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,15 @@ class LIVEKIT_API Room {
164164
/// - published tracks (audio/video/screen)
165165
/// - identity, SID, metadata
166166
/// - publishing/unpublishing operations
167-
/// @return Non-null pointer after successful connect().
167+
/// @return Non-null pointer while connected; nullptr before connect, after
168+
/// room end-of-stream teardown, or when the room is destroyed.
168169
LocalParticipant* localParticipant() const;
169170

170171
/// Look up a remote participant by identity.
171172
///
172173
/// @param identity The participant’s identity string (not SID)
173-
/// @return Pointer to RemoteParticipant if present, otherwise nullptr.
174+
/// @return Pointer to RemoteParticipant if present, otherwise nullptr (also
175+
/// nullptr after room teardown or when the room is destroyed).
174176
/// RemoteParticipant contains:
175177
/// - identity/name/metadata
176178
/// - track publications
@@ -181,7 +183,7 @@ class LIVEKIT_API Room {
181183
///
182184
/// @return Vector of non-null pointers to the current remote participants.
183185
/// The pointers are owned by the Room and remain valid until the
184-
/// corresponding participant disconnects.
186+
/// corresponding participant disconnects or the room is torn down.
185187
std::vector<RemoteParticipant*> remoteParticipants() const;
186188

187189
/// Returns the current connection state of the room.

‎src/room.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,11 @@ void Room::onEvent(const FfiEvent& event) {
12481248
FfiClient::instance().removeListener(listener_to_remove);
12491249
}
12501250

1251-
// Old state will be destroyed here when going out of scope
1251+
if (old_local_participant) {
1252+
old_local_participant->shutdown();
1253+
}
1254+
1255+
// old_* state is destroyed here when going out of scope
12521256

12531257
const RoomEosEvent ev;
12541258
if (delegate_snapshot) {

‎src/tests/unit/test_room.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ TEST_F(RoomTest, ConnectWithoutInitialize) {
3333
Room room;
3434
bool result = room.connect("wss://localhost:7880", "test", livekit::RoomOptions());
3535
EXPECT_FALSE(result) << "Connecting without initializing should return false";
36+
EXPECT_EQ(room.localParticipant(), nullptr)
37+
<< "Local participant should be null after failed connect";
38+
EXPECT_TRUE(room.remoteParticipants().empty())
39+
<< "Remote participants should be empty after failed connect";
3640
}
3741

3842
TEST_F(RoomTest, CreateRoom) {

0 commit comments

Comments
 (0)