From 2382b5cbd3bcf53f83807b0160c897b5c8a6fb9a Mon Sep 17 00:00:00 2001 From: Steve Pham <82231385+spham-amzn@users.noreply.github.com> Date: Tue, 14 Sep 2021 12:42:16 -0700 Subject: [PATCH] Linux fix launch project manager from editor (#4105) Signed-off-by: Steve Pham --- .../Process/ProcessWatcher_Linux.cpp | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/Code/Framework/AzFramework/Platform/Linux/AzFramework/Process/ProcessWatcher_Linux.cpp b/Code/Framework/AzFramework/Platform/Linux/AzFramework/Process/ProcessWatcher_Linux.cpp index 2d29fac73d..d2cd681012 100644 --- a/Code/Framework/AzFramework/Platform/Linux/AzFramework/Process/ProcessWatcher_Linux.cpp +++ b/Code/Framework/AzFramework/Platform/Linux/AzFramework/Process/ProcessWatcher_Linux.cpp @@ -263,21 +263,29 @@ namespace AzFramework } commandAndArgs[commandTokens.size()] = nullptr; + AZStd::vector> environmentVariablesManaged; + AZStd::vector environmentVariablesVector; char** environmentVariables = nullptr; - int numEnvironmentVars = 0; if (processLaunchInfo.m_environmentVariables) { - numEnvironmentVars = processLaunchInfo.m_environmentVariables->size(); - // Adding one more as exec expects the array to have a nullptr as the last element - environmentVariables = new char*[numEnvironmentVars + 1]; - for (int i = 0; i < numEnvironmentVars; i++) + for (const auto& envVarString : *processLaunchInfo.m_environmentVariables) { - const AZStd::string& envVarString = processLaunchInfo.m_environmentVariables->at(i); - environmentVariables[i] = new char[envVarString.size() + 1]; - environmentVariables[i][0] = '\0'; - azstrcat(environmentVariables[i], envVarString.size(), envVarString.c_str()); + auto& environmentVariable = environmentVariablesManaged.emplace_back(AZStd::make_unique(envVarString.size() + 1)); + environmentVariable[0] = '\0'; + azstrcat(environmentVariable.get(), envVarString.size() + 1, envVarString.c_str()); + environmentVariablesVector.emplace_back(environmentVariable.get()); } - environmentVariables[numEnvironmentVars] = nullptr; + // Adding one more as exec expects the array to have a nullptr as the last element + environmentVariablesVector.emplace_back(nullptr); + environmentVariables = environmentVariablesVector.data(); + } + else + { + // If no environment variables were specified, then use the current process's environment variables + // and pass it along for the execute . + extern char **environ; // Defined in unistd.h + environmentVariables = ::environ; + AZ_Assert(environmentVariables, "Environment variables for current process not available\n"); } pid_t child_pid = fork(); @@ -290,15 +298,6 @@ namespace AzFramework // Close these handles as they are only to be used by the child process processData.m_startupInfo.CloseAllHandles(); - if (processLaunchInfo.m_environmentVariables) - { - for (int i = 0; i < numEnvironmentVars; i++) - { - delete [] environmentVariables[i]; - } - delete [] environmentVariables; - } - for (int i = 0; i < commandTokens.size(); i++) { delete [] commandAndArgs[i];