Skip to content

Commit

Permalink
Fix some ways to get crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
KGronek-Pubnub committed Oct 3, 2024
1 parent 8c357db commit b879125
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Source/PubnubLibrary/Private/PubnubSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@ void UPubnubSubsystem::InitPubnub()
PubnubError("Pubnub is already initialized", EPubnubErrorType::PET_Warning);
return;
}

//Create new threads - separate for subscribe and all other operations
QuickActionThread = new FPubnubFunctionThread;
LongpollThread = new FPubnubLoopingThread;

if(!CheckQuickActionThreadValidity())
{return;}

QuickActionThread->AddFunctionToQueue( [this]
InitPubnub_priv();

//If initialized correctly, create required thread.
if(IsInitialized)
{
InitPubnub_priv();
});
//Create new threads - separate for subscribe and all other operations
QuickActionThread = new FPubnubFunctionThread;
LongpollThread = new FPubnubLoopingThread;
}

}

void UPubnubSubsystem::DeinitPubnub()
{
if(!CheckQuickActionThreadValidity())
{return;}
if(!QuickActionThread)
{
DeinitPubnub_priv();
return;
}

QuickActionThread->AddFunctionToQueue( [this]
{
Expand Down Expand Up @@ -828,6 +830,11 @@ void UPubnubSubsystem::PubnubError(FString ErrorMessage, EPubnubErrorType ErrorT
UE_LOG(PubnubLog, Warning, TEXT("%s"), *ErrorMessage);
}

if(!OnPubnubError.IsBound())
{
return;
}

//Errors has to be broadcasted on GameThread, otherwise engine will crash if someone uses them for example with widgets
AsyncTask(ENamedThreads::GameThread, [this, ErrorMessage, ErrorType]()
{
Expand Down Expand Up @@ -959,11 +966,13 @@ void UPubnubSubsystem::InitPubnub_priv()
if(std::strlen(PublishKey) == 0 )
{
PubnubError("Publish key is empty, can't initialize Pubnub");
return;
}

if(std::strlen(SubscribeKey) == 0 )
{
PubnubError("Subscribe key is empty, can't initialize Pubnub");
return;
}

ctx_pub = pubnub_alloc();
Expand Down Expand Up @@ -1179,8 +1188,11 @@ void UPubnubSubsystem::UnsubscribeFromAll_priv()
}

LongpollThread->ClearLoopingFunctions();

SystemPublish(ChannelForSystemPublish);

if(!ChannelForSystemPublish.IsEmpty())
{
SystemPublish(ChannelForSystemPublish);
}
}

void UPubnubSubsystem::AddChannelToGroup_priv(FString ChannelName, FString ChannelGroup)
Expand Down

0 comments on commit b879125

Please sign in to comment.