Pointer Memory Getting Out of Scope. Unable to hold it

Hi, I have a server development build where I’m loading files from my module. This scenario is working perfectly fine when I run the build from the uproject file. When I run the same from development build I’m getting null reference, means that object is not getting created on the other end.
I’ve added the screenshot of the project. Have a look, I’ve added for both of the scenarios. Below is the package debugging information where you can see OriginalFileName and FileName columns are empty.

And below is the scenario where I run the uproject file. And it works fine in the below condition. I found the reference of the dll.

How should I build project so that my custom module can be used in the package as well. 

I’ve added the target.cs files of both of my game packages (one package is server and other is gameclient )

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class VizGame : ModuleRules
{
    public VizGame(TargetInfo Target)
    {
        PrivateIncludePaths.AddRange(
            new string[] { 
                "VizGame/Classes/Player",
                "VizGame/Private",
                "VizGame/Private/UI",
                "VizGame/Private/UI/Menu",
                "VizGame/Private/UI/Style",
                "VizGame/Private/UI/Widgets",
            }
        );

        PublicDependencyModuleNames.AddRange(
            new string[] {
                "Core",
                "CoreUObject",
                "Engine",
                "OnlineSubsystem",
                "OnlineSubsystemUtils",
                "AssetRegistry",
                "AIModule",
                "GameplayTasks",
                "XmlParser",
                "UMG",
                "VizNetwork",
                "Voice",
                "CISQLite3",
                "Landscape",
            }
        );

        PrivateDependencyModuleNames.AddRange(
            new string[] {
                "InputCore",
                "Slate",
                "SlateCore",
                "VizGameLoadingScreen",
                "Json",
                "ProceduralMeshComponent",
                "Landscape"
            }
        );

        DynamicallyLoadedModuleNames.AddRange(
            new string[] {
                "OnlineSubsystemNull",
                "NetworkReplayStreaming",
                "NullNetworkReplayStreaming",
                "HttpNetworkReplayStreaming"
            }
        );

        PrivateIncludePathModuleNames.AddRange(
            new string[] {
                "NetworkReplayStreaming"
            }
        );

        if ((Target.Platform == UnrealTargetPlatform.Win32) || (Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Linux) || (Target.Platform == UnrealTargetPlatform.Mac))
        {
            
        }
        else if (Target.Platform == UnrealTargetPlatform.PS4)
        {
            DynamicallyLoadedModuleNames.Add("OnlineSubsystemPS4");
        }
        else if (Target.Platform == UnrealTargetPlatform.XboxOne)
        {
            DynamicallyLoadedModuleNames.Add("OnlineSubsystemLive");
        }
    }
}

and

// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.Collections.Generic;

public class VizGameServerTarget : TargetRules
{
    public VizGameServerTarget(TargetInfo Target)
    {
        Type = TargetType.Server;
        //bUsesSteam = true;
    }

    //
    // TargetRules interface.
    //

    public override bool GetSupportedPlatforms(ref List<UnrealTargetPlatform> OutPlatforms)
    {
        // It is valid for only server platforms
        return UnrealBuildTool.UnrealBuildTool.GetAllServerPlatforms(ref OutPlatforms, false);
    }

    public override void SetupBinaries(
        TargetInfo Target,
        ref List<UEBuildBinaryConfiguration> OutBuildBinaryConfigurations,
        ref List<string> OutExtraModuleNames
        )
    {
        OutExtraModuleNames.Add("VizGame");
        OutExtraModuleNames.Add("VizServer");
        OutExtraModuleNames.Add("VizServerNull");
        OutExtraModuleNames.Add("VizNetwork");
    }
}

Below is the Build.cs files of my Client (VizGame)
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.Collections.Generic;

public class VizGameTarget : TargetRules
{
public VizGameTarget(TargetInfo Target)
{
Type = TargetType.Game;
bUsesSteam = true;
}

//
// TargetRules interface.
//

public override void SetupBinaries(
    TargetInfo Target,
    ref List<UEBuildBinaryConfiguration> OutBuildBinaryConfigurations,
    ref List<string> OutExtraModuleNames
    )
{
    OutExtraModuleNames.Add("VizGame");
    OutExtraModuleNames.Add("VizServerNull");
}

public override void SetupGlobalEnvironment(
        TargetInfo Target,
        ref LinkEnvironmentConfiguration OutLinkEnvironmentConfiguration,
        ref CPPEnvironmentConfiguration OutCPPEnvironmentConfiguration
        )
{
    if (Target.Platform == UnrealTargetPlatform.PS4)
    {
        OutCPPEnvironmentConfiguration.Definitions.Add("GARLIC_HEAP_SIZE=(2600ULL * 1024 * 1024)");
        OutCPPEnvironmentConfiguration.Definitions.Add("ONION_HEAP_SIZE=(200ULL * 1024 * 1024)");
        OutCPPEnvironmentConfiguration.Definitions.Add("RESERVED_MEMORY_SIZE=(1ULL * 1024 * 1024)");
        OutCPPEnvironmentConfiguration.Definitions.Add("LIBC_MALLOC_SIZE=(32ULL * 1024 * 1024)");
        OutCPPEnvironmentConfiguration.Definitions.Add("LIBC_MALLOC_SIZE=(32ULL * 1024 * 1024)");

        //for a real game these could be behind a call to a virtual function defined in a partial class in a protected folder also.
        OutCPPEnvironmentConfiguration.Definitions.Add("UE4_PROJECT_NPTITLEID=NPXX51358_00");
        OutCPPEnvironmentConfiguration.Definitions.Add("UE4_PROJECT_NPTITLESECRET=81ae213eafbc64777574955bf921c9be3c60a3bddef70c357d8fe49ad64e0d0402d2249de390174832c5e4098114c93c33705b597cfbe9b1153d58fe9fae1f0de1466daf18ef25d06122cff7c95bde07bc060109e20c865305692dfbf9d7b726460893c4abd86dc9e8fd6b5db7dca4ffd4eefcb1771baccd576109bea862d6d4");
    }
}

}

and Server(VizGameServer)
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.Collections.Generic;

public class VizGameServerTarget : TargetRules
{
public VizGameServerTarget(TargetInfo Target)
{
Type = TargetType.Server;
//bUsesSteam = true;
}

//
// TargetRules interface.
//

public override bool GetSupportedPlatforms(ref List<UnrealTargetPlatform> OutPlatforms)
{
    // It is valid for only server platforms
    return UnrealBuildTool.UnrealBuildTool.GetAllServerPlatforms(ref OutPlatforms, false);
}

public override void SetupBinaries(
    TargetInfo Target,
    ref List<UEBuildBinaryConfiguration> OutBuildBinaryConfigurations,
    ref List<string> OutExtraModuleNames
    )
{
    OutExtraModuleNames.Add("VizGame");
    OutExtraModuleNames.Add("VizServer");
    OutExtraModuleNames.Add("VizServerNull");
    OutExtraModuleNames.Add("VizNetwork");
}

}

Any updates Unreal Devs? Its a critical problem here ! I’m very much stuck. Need help

Hi Unreal Devs, I’ve dug more into the problem and rephrased the problem statement. Apart from the above file where I’m suppose to add all the modules name, an ambiguous behavior caught my attention. I would like to know more about this chain of call leading to such behavior.

I have 2 modules with me. “VizNetwork” and “VizServer”. These modules contain the services which are need for initializing dedicated server services for my game.

  • Here is VizNetworkModule header and cpp file.

VizNetwork.h

#pragma once

#include "ModuleInterface.h"

class IVizNetwork;

class VIZNETWORK_API FVizNetworkModule : public IModuleInterface
{

public:

    FVizNetworkModule();

    virtual void                            StartupModule() override;
    virtual void                            ShutdownModule() override;

    void                                    RegisterNetwork(IVizNetwork * pNetwork);
    IVizNetwork *                           GetNetwork() const;

    IVizNetwork *                            m_pNetwork;
    FString                                    m_strActiveModuleName;
};

VizNetwork.cpp

#include "VizNetwork.h"

IMPLEMENT_GAME_MODULE(FVizNetworkModule, VizNetwork);

FVizNetworkModule::FVizNetworkModule()
    :m_pNetwork(nullptr)
{

}

void FVizNetworkModule::StartupModule()
{
    if (IsRunningDedicatedServer())
    {
        m_strActiveModuleName = "VizServer";
		UE_LOG(LogTemp, Log, TEXT(" VizNetwork Module Started loading. Trying to load VizServer Module"));
    }
    else
    {
        m_strActiveModuleName = "VizServerNull";
		UE_LOG(LogTemp, Log, TEXT(" VizNetwork Module Started loading. Trying to load VizServerNull Module"));
    }
	

    FModuleManager & ModuleManager = FModuleManager::Get();
    check(ModuleManager.LoadModule(*m_strActiveModuleName).IsValid());
}

void FVizNetworkModule::ShutdownModule()
{
	UE_LOG(LogTemp, Log, TEXT(" VizNetwork Module Stoppped!"));
    FModuleManager & ModuleManager = FModuleManager::Get();
    ModuleManager.UnloadModule(*m_strActiveModuleName);
}

void FVizNetworkModule::RegisterNetwork(IVizNetwork * pNetwork)
{
    m_pNetwork = pNetwork;
}

IVizNetwork * FVizNetworkModule::GetNetwork() const
{
    return m_pNetwork;
}
  • Here is the VizServerModule header and cpp file.

VizServerModule.h

#pragma once

#include "ModuleInterface.h"
#include"D:/EpicGames/UnrealEngine 4.14/Engine/Source/Runtime/Core/Public/Modules/ModuleManager.h"

class UVizServer;

extern UVizServer * m_pServer_saved;

//#define DllExport   __declspec( dllexport )

class VIZSERVER_API FVizServerModule : public IModuleInterface
{
public:
	FVizServerModule();
    virtual void                            StartupModule() override;
    virtual void                            ShutdownModule() override;
	UVizServer *                           GetNetwork() const;

    UVizServer *                            m_pServer;
};

VizServerModule.cpp

#include "VizServer.h"
#include "VizServerModule.h"

UVizServer * m_pServer_saved = nullptr;

IMPLEMENT_GAME_MODULE(FVizServerModule, VizServer);

FVizServerModule::FVizServerModule()
	:m_pServer(nullptr)
{

}


void FVizServerModule::StartupModule()
{
    m_pServer = NewObject<UVizServer>();
    FVizNetworkModule & GameNetworkModule = FModuleManager::GetModuleChecked<FVizNetworkModule>(TEXT("VizNetwork"));
    GameNetworkModule.RegisterNetwork(m_pServer);
	m_pServer_saved = m_pServer;
	UE_LOG(LogTemp, Log, TEXT(" VizNetwork Module Registered. In VizServer Module"));
}

void FVizServerModule::ShutdownModule()
{
    delete m_pServer;
}

UVizServer*  FVizServerModule::GetNetwork() const
{
	//return m_pServer_saved;
	return m_pServer;
}

And these modules are loaded at the start of the first map VizGameMode_Menu. I’m adding the snippet where they are added in the code

void AVizGameMode_Menu::InitGame(const FString & MapName, const FString & Options, FString & ErrorMessage)
{
    Super::InitGame(MapName, Options, ErrorMessage);

	//#if VIZ_SERVER_ENABLED
	V_LOGM(LogViz, "GameMode_Menu Options = %s", *Options);
	//FVizServerModule & NetworkModule = nullptr;
	FVizNetworkModule & NetworkModule = FModuleManager::GetModuleChecked<FVizNetworkModule>("VizNetwork");
	//FVizServerModule & serverModule = FModuleManager::GetModuleChecked<FVizServerModule>("VizServer");

	serverType = UGameplayStatics::ParseOption(Options, PARSE_SERVERTYPE);

	//Adding this check So that DataBase is initialize in Singleplayer/Standalone mode also
	// initialize database
	if (!InitializeDatabase())
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, "Database Initialization Failed");
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, "Database Initialization Successful");
	}

    if (serverType == "lobbyserver")
    {
		m_pLobbyServer = NetworkModule.GetNetwork()->CreateLobbyServer();

		if (m_pLobbyServer == nullptr)
			return;

		m_pLobbyServer->Init(this);
        if (m_pLobbyServer->StartServer("LobbyServerListenSocket", LOBBY_SERVER_IP, LOBBY_SERVER_TCP_LISTEN_PORT))
        {
            V_LOG(LogViz, "Lobby Server Created");
        }
        else
        {
            V_LOG(LogViz, TEXT("Lobby Server Not Created"));
        }

        if (!LoadUserDetails())
        {
            V_LOG(LogViz, TEXT("LobbyServer : Login Details of players could not be read"));
        }
    }

	
	else if(serverType == "gameserver")
    {
   //     m_pGameServer = NetworkModule.GetNetwork()->CreateGameServer();
		m_pGameServer = NetworkModule.GetNetwork()->CreateGameServer();
        
		if (m_pGameServer == nullptr)
			return;

		m_pGameServer->Init(this);
        if (!m_pGameServer->IsConnectedToLobbyServer())
        {
            m_pGameServer->StartServer("gsSocket", "127.0.0.1", 5567);
            if (m_pGameServer->ConnectToLobbyServer(LOBBY_SERVER_IP, LOBBY_SERVER_TCP_LISTEN_PORT))
            {
                V_LOG(LogViz, TEXT("Game Server Created"));
            }
            else
            {
                V_LOG(LogViz, TEXT("Game Server Not Created"));
            }
        }

		else
		{
			TArray<uint8> buff;
			FMemoryWriter mw(buff);
			TEnumAsByte<GameServerToLobbyServerMsgs::Type> msgType = GameServerToLobbyServerMsgs::GSLS_GAME_READY_TO_START;
			mw << msgType;
			m_pGameServer->SendMsgToLobbyServer(buff);
		}
    }

    CFileHelper::GetFilesRecursively(CFileHelper::GetSavedGameDirectory(), m_aSavedFileNames);
}

The line of code which is creating error is:

  • FVizNetworkModule & NetworkModule = FModuleManager::GetModuleChecked(“VizNetwork”);

After this line executes, it creates the Module Objects and by the time it exits execution to the next line, it releases the memory pointers of module objects.

I’ve added the diagram to show the call map when the object is initialized and when the objects memory is getting free.

Desired Result is I want my module objects to persist in the level so that I can start the services present in them. How do I achieve it ?

I’ve tried following code to fix this problem. I tackled this problem by adding this pointer with AddToRoot() function. This method doesn’t allow that pointer to be removed. Hence I’m still able to use it.

m_pServer->AddToRoot();

and

UPROPERTY()
    UVizServer *                            m_pServer;