I am trying to use Azure storage sdk C++ library with Unreal Engine 4 to upload images to the Azure cloud. Library is built with vcpkg and dlls are linked dynamically. Here is a simplified example of the code I use.
THIRD_PARTY_INCLUDES_START
#pragma warning(disable:4668)
#pragma warning(disable:4005) // 'TEXT': macro redefinition
#include <was/storage_account.h>
#include <was/blob.h>
#include <cpprest/filestream.h>
#include <cpprest/containerstream.h>
THIRD_PARTY_INCLUDES_END
const utility::string_t storage_connection_string(U("DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=MyAccountKey;EndpointSuffix=core.windows.net"));
void AAwsTest2GameModeBase::StartPlay() {
try
{
// Retrieve storage account from connection string.
azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string);
// Create the blob client.
azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client();
// Retrieve a reference to a container.
azure::storage::cloud_blob_container container = blob_client.get_container_reference(U("image-container"));
azure::storage::cloud_block_blob blockBlob = container.get_block_blob_reference(U("28bbcdb0b3e5417b207572e292ae98412cd9d931eae6266f7c4fd788ad8544a20.jpg"));
concurrency::streams::istream input_stream = concurrency::streams::file_stream<uint8_t>::open_istream(U("image.jpg")).get();
blockBlob.upload_from_stream(input_stream);
input_stream.close().wait();
}
catch (const std::exception& e)
{
std::wcout << U("Error: ") << e.what() << std::endl;
}
}
Build.cs content
public class Program : ModuleRules
{
public Program(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
//I tried to use ansi allocator but it did not help
//PublicDefinitions.Add("FORCE_ANSI_ALLOCATOR");
PublicIncludePaths.Add(Path.Combine(DependencyFolderWin, "include"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "wastorage.lib"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "cpprest_2_10.lib"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "brotlicommon.lib"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "brotlidec.lib"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "brotlienc.lib"));
PublicAdditionalLibraries.Add(Path.Combine(DependencyFolderWin, "lib", "zlib.lib"));
}
private string DependencyFolderWin
{
get
{
string moduleDir = Path.GetFullPath(ModuleDirectory);
return Path.Combine(moduleDir, "./../../deps");
}
}
}
The problem is that it successfully uploads image and crashes right after return from this function (in this case StartPlay() or Upload() as in the callstack) with this callstack:
UE4Editor-Core.dll!00007ff9e620ff1b() Unknown
UE4Editor-Core.dll!00007ff9e62109e3() Unknown
UE4Editor-Core.dll!00007ff9e6211592() Unknown
UE4Editor-Core.dll!00007ff9e5e95277() Unknown
[Inline Frame] UE4Editor-MsgQueuePlugin.dll!operator delete(void *) Line 31 C++
[Inline Frame] UE4Editor-MsgQueuePlugin.dll!std::_Deallocate(void * _Ptr, unsigned __int64 _Bytes) Line 207 C++
[Inline Frame] UE4Editor-MsgQueuePlugin.dll!std::allocator<wchar_t>::deallocate(wchar_t * const) Line 992 C++
UE4Editor-MsgQueuePlugin.dll!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::_Tidy_deallocate() Line 3992 C++
[Inline Frame] UE4Editor-MsgQueuePlugin.dll!std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >::{dtor}() Line 2460 C++
UE4Editor-MsgQueuePlugin.dll!web::uri::~uri() C++
UE4Editor-MsgQueuePlugin.dll!azure::storage::cloud_blob::~cloud_blob() C++
UE4Editor-MsgQueuePlugin.dll!AzureUploader::Upload(TArray<unsigned char,FDefaultAllocator> file, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & path, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & fileName) Line 37 C++
Callstack with Editor debug symbols:
[Inline Frame] UE4Editor-Core.dll!__TBB_machine_cmpswp1(volatile void *) Line 69 C++
[Inline Frame] UE4Editor-Core.dll!__TBB_TryLockByte(unsigned char &) Line 917 C++
UE4Editor-Core.dll!__TBB_LockByte(unsigned char & flag) Line 924 C++
[Inline Frame] UE4Editor-Core.dll!MallocMutex::scoped_lock::{ctor}(MallocMutex &) Line 66 C++
UE4Editor-Core.dll!rml::internal::Block::freePublicObject(rml::internal::FreeObject * objectToFree) Line 1382 C++
[Inline Frame] UE4Editor-Core.dll!rml::internal::internalPoolFree(rml::internal::MemoryPool * memPool, void *) Line 2571 C++
UE4Editor-Core.dll!rml::internal::internalFree(void * object) Line 2595 C++
UE4Editor-Core.dll!FMemory::Free(void * Original) Line 76 C++
[External Code]
UE4Editor-MsgQueuePlugin.dll!AzureUploader::Upload(TArray<unsigned char,FDefaultAllocator> file, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & path, const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & fileName) Line 37 C++
Looks like it crashes while deleting uri from some structure. Probably, the problem is with unreal memory management. I tried to use ansi allocator but it did not help. Any ideas how to make it work properly? Thank you
Finally, it was solved by using UE4 plugin from this example. It is not the solution I wanted but at least it works. There are statically linked libraries and hardcoded compiler version which may bring problems in the future.