Skip to content

Commit

Permalink
fix: avoid memory leak by moving Api object to the module singleton (#12
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Zino2201 authored Oct 19, 2024
1 parent 095b5a6 commit 0ff4b56
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Source/BlueprintUe/Private/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ void Api::CreateBlueprint(TFunction<void(FString, TSharedPtr<FJsonObject>)> OnSu
HttpRequest->SetURL(Settings->Hostname + "/api/upload");
HttpRequest->SetVerb("POST");
HttpRequest->SetTimeout(1000);
HttpRequest->SetHeader("X-Token",Settings->ApiKey);
HttpRequest->SetHeader("Content-Type","application/x-www-form-urlencoded");
HttpRequest->SetHeader("X-Token", Settings->ApiKey);
HttpRequest->SetHeader("Content-Type", "application/x-www-form-urlencoded");

Exposure = Exposure.ToLower();
if (Expiration == "Never")
Expand All @@ -34,7 +34,7 @@ void Api::CreateBlueprint(TFunction<void(FString, TSharedPtr<FJsonObject>)> OnSu
{
Expiration = "604800";
}

const FString PostContent = FString::Printf(TEXT("title=%s&exposure=%s&expiration=%s&version=%s&blueprint=%s"),
*FGenericPlatformHttp::UrlEncode(Title),
*FGenericPlatformHttp::UrlEncode(Exposure),
Expand All @@ -43,7 +43,7 @@ void Api::CreateBlueprint(TFunction<void(FString, TSharedPtr<FJsonObject>)> OnSu
*FGenericPlatformHttp::UrlEncode(Blueprint)
);
HttpRequest->SetContentAsString(PostContent);

HttpRequest->ProcessRequest();
}

Expand All @@ -54,11 +54,11 @@ void Api::OnProcessRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Res
OnError("error");
return;
}

TSharedPtr<FJsonObject> JSONObject;
const TSharedRef<TJsonReader<>> JSONReader = TJsonReaderFactory<>::Create(*Response->GetContentAsString());
FJsonSerializer::Deserialize(JSONReader, JSONObject);

if (Response->GetResponseCode() >= 400)
{
if (JSONObject)
Expand Down
5 changes: 3 additions & 2 deletions Source/BlueprintUe/Private/SCreateBlueprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Api.h"
#include "Widgets/Input/SMultiLineEditableTextBox.h"
#include "Widgets/Layout/SScrollBox.h"
#include "BlueprintUe.h"

void SCreateBlueprint::Construct( const FArguments& InArgs )
{
Expand Down Expand Up @@ -276,8 +277,8 @@ FReply SCreateBlueprint::CreateFromSlate()
CurrentError = "";
CreateButton->SetEnabled(false);

Api* API = new Api();
API->CreateBlueprint(OnAPISuccess, OnAPIError, Title, *CurrentExposure, *CurrentExpiration, *CurrentUEVersion, Blueprint);
Api& Api = FBlueprintUeModule::Get().GetApi();
Api.CreateBlueprint(OnAPISuccess, OnAPIError, Title, *CurrentExposure, *CurrentExpiration, *CurrentUEVersion, Blueprint);

return FReply::Handled();
}
2 changes: 1 addition & 1 deletion Source/BlueprintUe/Public/Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Api
Api(){
Settings = GetMutableDefault<UBlueprintUeSettings>();
}
void CreateBlueprint(TFunction<void(FString, TSharedPtr<FJsonObject>)> OnSuccess, TFunction<void(FString)> OnError, FString Title, FString Exposure, FString Expiration, FString UEVersion, FString Blueprint);

void CreateBlueprint(TFunction<void(FString, TSharedPtr<FJsonObject>)> OnSuccess, TFunction<void(FString)> OnError, FString Title, FString Exposure, FString Expiration, FString UEVersion, FString Blueprint);
private:
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> HttpRequest = FHttpModule::Get().CreateRequest();
void OnProcessRequestComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
Expand Down
12 changes: 10 additions & 2 deletions Source/BlueprintUe/Public/BlueprintUe.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CoreMinimal.h"
#include "HttpModule.h"
#include "Api.h"

class FToolBarBuilder;
class FMenuBuilder;
Expand All @@ -14,14 +15,21 @@ class FBlueprintUeModule : public IModuleInterface
virtual void ShutdownModule() override;

void PluginButtonClicked();

private:

Api& GetApi() { return Api; }

static FBlueprintUeModule& Get()
{
static const FName ModuleName = "BlueprintUe";
return FModuleManager::LoadModuleChecked<FBlueprintUeModule>(ModuleName);
}
private:
void RegisterMenus();
void RegisterSettings();
void UnregisterSettings();
bool HandleSettingsSaved();

TSharedRef<class SDockTab> OnSpawnPluginTab(const class FSpawnTabArgs& SpawnTabArgs);
TSharedPtr<class FUICommandList> PluginCommands;
Api Api;
};

0 comments on commit 0ff4b56

Please sign in to comment.