Skip to content

Commit

Permalink
Fixed memory leaks in several modules where GameEvents were being us…
Browse files Browse the repository at this point in the history
…ed and not released
  • Loading branch information
linuxgurugamer committed Aug 22, 2022
1 parent b871648 commit a03d7df
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 48 deletions.
3 changes: 3 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,6 @@ Taken over by LinuxGurugamer here
Changed OnGUI to FixedUpdate in Speedometer.cs
Added AssemblyFileVersion
Updated version file for 1.12

1.9.17
Fixed memory leaks in several modules where GameEvents were being used and not released
9 changes: 7 additions & 2 deletions GameData/SmartParts/SmartParts.version
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
"VERSION": {
"MAJOR": 1,
"MINOR": 9,
"PATCH": 16,
"PATCH": 17,
"BUILD": 0
},
"KSP_VERSION": {
"MAJOR": 1,
"MINOR": 12,
"PATCH": 2
},
"KSP_VERSION_MIN": {
"MAJOR": 1,
"MINOR": 8,
"MINOR": 12,
"PATCH": 0
}
}
4 changes: 2 additions & 2 deletions SmartParts.version
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"VERSION": {
"MAJOR": 1,
"MINOR": 9,
"PATCH": 16,
"BUILD": 2
"PATCH": 17,
"BUILD": 0
},
"KSP_VERSION": {
"MAJOR": 1,
Expand Down
4 changes: 2 additions & 2 deletions SmartParts/AssemblyVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

using System.Reflection;

[assembly: AssemblyVersion("1.9.16.1")]
[assembly: AssemblyFileVersion("1.9.16.1")]
[assembly: AssemblyVersion("1.9.17.0")]
[assembly: AssemblyFileVersion("1.9.17.0")]
8 changes: 8 additions & 0 deletions SmartParts/DPLD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ private void Start()
initLight(true, activeLight);
updateButtons();
}

void OnDestroy()
{
GameEvents.CommNet.OnCommHomeStatusChange.Remove(onCommHomeStatusChange);
GameEvents.CommNet.OnCommStatusChange.Remove(onCommStatusChange);
GameEvents.CommNet.OnNetworkInitialized.Remove(onNetworkInitialized);
}

private const int PHYSICSWAIT = 1;
int physicsCnt = 0;
public override void OnUpdate()
Expand Down
115 changes: 77 additions & 38 deletions SmartParts/ProxSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,43 @@ public void getCount() {

#region Overrides

public override void OnStart(StartState state) {
public override void OnStart(StartState state)
{
//Clear listeners if the scene changes. Will be recreated on new scene load
GameEvents.onGameSceneLoadRequested.Add(clearListeners);
//Redraw buttons
updateButtons();
if (state != StartState.Editor) {
if (state != StartState.Editor)
{
//Force activation of proximity sensor upon load/unpack
this.part.force_activate();
//Register the listener
registerListener();
}
}

public override void OnFixedUpdate() {
if(justRegistered){
public override void OnFixedUpdate()
{
if (justRegistered)
{
justRegistered = false;
}
//Update target distance and determine target window
updateDistance();
//If the device is armed, check for the trigger altitude
if (isArmed) {
if (isArmed)
{
//We're departing. Trigger at or beyond target distance
if (direction != "Approach" && departing && Math.Abs((currentDistance + currentWindow) - meterDistance) < currentWindow) {
if (direction != "Approach" && departing && Math.Abs((currentDistance + currentWindow) - meterDistance) < currentWindow)
{
Log.Info("Proximity alert. Action fired on " + this.vessel.name + " on channel " + this.channel);
//This flag is checked for in OnUpdate to trigger staging
fireNextupdate = true;
isArmed = false;
}
//We're approaching. Trigger at or closer than target distance
else if (direction != "Departure" && !departing && Math.Abs((currentDistance - currentWindow) - meterDistance) < currentWindow) {
else if (direction != "Departure" && !departing && Math.Abs((currentDistance - currentWindow) - meterDistance) < currentWindow)
{
Log.Info("Proximity alert. Action fired on " + this.vessel.name + " on channel " + this.channel);
//This flag is checked for in OnUpdate to trigger staging
fireNextupdate = true;
Expand All @@ -98,32 +105,41 @@ public override void OnFixedUpdate() {
}

//If auto reset is enabled, wait for departure from the target window and rearm
if (!isArmed & autoReset) {
if (!isArmed & autoReset) {
if (departing && Math.Abs((currentDistance + currentWindow) - meterDistance) > currentWindow) {
if (!isArmed & autoReset)
{
if (!isArmed & autoReset)
{
if (departing && Math.Abs((currentDistance + currentWindow) - meterDistance) > currentWindow)
{
Log.Info("Proximity sensor reset on " + this.vessel.name);
isArmed = true;
}
else if (!departing && Math.Abs((currentDistance - currentWindow) - meterDistance) > currentWindow) {
else if (!departing && Math.Abs((currentDistance - currentWindow) - meterDistance) > currentWindow)
{
Log.Info("Proximity sensor reset on " + this.vessel.name);
isArmed = true;
}
}
}
}

public override void OnUpdate() {
public override void OnUpdate()
{
//If this proximity sensor isn't registered, register it now
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == false) {
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == false)
{
registerListener();
}
//In order for physics to take effect on jettisoned parts, the staging event has to be fired from OnUpdate
if (fireNextupdate && !justRegistered) {
if (fireNextupdate && !justRegistered)
{
int groupToFire = 0; //AGX: need to send correct group
if (AGXInterface.AGExtInstalled()) {
if (AGXInterface.AGExtInstalled())
{
groupToFire = int.Parse(agxGroupType);
}
else {
else
{
groupToFire = int.Parse(group);
}
Helper.fireEvent(this.part, groupToFire, (int)agxGroupNum);
Expand All @@ -136,20 +152,23 @@ public override void OnUpdate() {
#region Events

[KSPAction("Activate Detection")]
public void doActivateAG(KSPActionParam param) {
public void doActivateAG(KSPActionParam param)
{
isArmed = true;
}

[KSPAction("Deactivate Detection")]
public void doDeActivateAG(KSPActionParam param) {
public void doDeActivateAG(KSPActionParam param)
{
isArmed = false;
}

#endregion

#region Methods

private void updateButtons() {
private void updateButtons()
{
//Change to AGX buttons if AGX installed
if (AGXInterface.AGExtInstalled())
{
Expand Down Expand Up @@ -184,7 +203,8 @@ private void updateButtons() {
}
}

private void updateDistance() {
private void updateDistance()
{
double testDistance = 0;
double lastDistance = 0;
ProxSensor closestSensor = null;
Expand All @@ -193,19 +213,23 @@ private void updateDistance() {
lastDistance = currentDistance;

//Find closest target on our channel and set closestDistance
foreach (var listener in ProxChannel.Listeners.ToList()) {
if (this.vessel.id != listener.vessel.id) {
foreach (var listener in ProxChannel.Listeners.ToList())
{
if (this.vessel.id != listener.vessel.id)
{
testDistance = Vector3d.Distance(this.vessel.GetWorldPos3D(), listener.vessel.GetWorldPos3D());
//Set distance and listener to the values from the closest non-self sensor on the same channel as us
if (listener.channel == this.channel && (testDistance < currentDistance || closestSensor == null)) {
if (listener.channel == this.channel && (testDistance < currentDistance || closestSensor == null))
{
closestSensor = listener;
currentDistance = testDistance;
}
}
}

//If no sensors detected, proceed no further
if (closestSensor == null) {
if (closestSensor == null)
{
return;
}

Expand All @@ -216,39 +240,48 @@ private void updateDistance() {
//If the target was just registered (AKA just entered 2.5km), it's velocity measurements are innacurate. Manually set to 0 until next pass.
currentWindow = (closestSensor.justRegistered ? 0 : Math.Abs(currentDistance - lastDistance) * 1.05);
//We now have one data point. Remove the justRegistered flag for the next pass
if (closestSensor.justRegistered) {
if (closestSensor.justRegistered)
{
Log.Info(closestSensor.vessel.name + " inelligible for proximity detection this time. Waiting for next pass.");
}
}

public void Update() { //AGX: The OnUpdate above only seems to run in flight mode, Update() here runs in all scenes
public void Update()
{ //AGX: The OnUpdate above only seems to run in flight mode, Update() here runs in all scenes
if (agxGroupType == "1" & groupLastUpdate != "1" || agxGroupType != "1" & groupLastUpdate == "1") //AGX: Monitor group to see if we need to refresh window
{
updateButtons();
refreshPartWindow();
if (agxGroupType == "1") {
if (agxGroupType == "1")
{
groupLastUpdate = "1";
}
else {
else
{
groupLastUpdate = "0";
}
}
}

public void OnDestroy() {
public void OnDestroy()
{
GameEvents.onGameSceneLoadRequested.Remove(clearListeners);
GameEvents.onGameSceneLoadRequested.Remove(clearListeners);
deregisterListener(this);
}

public void clearListeners(GameScenes scene) {
public void clearListeners(GameScenes scene)
{
//On scene change, clear out all of the registered listeners
ProxChannel.Listeners.Clear();
ProxChannel.Listeners.TrimExcess();
}

public void registerListener() {
public void registerListener()
{
//Remove duplicate entries from the list
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == true) {
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == true)
{
return;
}
Log.Info(this.vessel.vesselName + " proximity alarm has been registered on channel " + this.channel);
Expand All @@ -257,28 +290,34 @@ public void registerListener() {
justRegistered = true;
}

public void deregisterListener(ProxSensor sensor) {
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == true) {
public void deregisterListener(ProxSensor sensor)
{
if (ProxChannel.Listeners.Any(listener => listener.vessel.id == this.vessel.id && listener.channel == this.channel) == true)
{
Log.Info(sensor.vessel.vesselName + " proximity alarm has been deregistered on channel " + sensor.channel);
ProxChannel.Listeners.Remove(sensor);
ProxChannel.Listeners.TrimExcess();
}
}

private void OnDetach(bool first) {
private void OnDetach(bool first)
{
//Remove this prox sensor from listener list
deregisterListener(this);
}

public void onGUI() {
public void onGUI()
{
//Update buttons
updateButtons();
}

private void refreshPartWindow() { //AGX: Refresh right-click part window to show/hide Groups slider
private void refreshPartWindow()
{ //AGX: Refresh right-click part window to show/hide Groups slider
UIPartActionWindow[] partWins = FindObjectsOfType<UIPartActionWindow>();
//Log.Info("Wind count " + partWins.Count());
foreach (UIPartActionWindow partWin in partWins) {
foreach (UIPartActionWindow partWin in partWins)
{
partWin.displayDirty = true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions SmartParts/SmartParts.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{668B7336-C342-4CE6-8BF4-B937C6FF9E37}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>SmartParts</RootNamespace>
<AssemblyName>SmartParts</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
2 changes: 1 addition & 1 deletion SmartParts/SmartSRB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void OnEditorPartPlaced(Part p)
CheckEngine();
}

void Destroy()
void OnDestroy()
{
GameEvents.onEditorPartPlaced.Remove(OnEditorPartPlaced);
}
Expand Down
5 changes: 5 additions & 0 deletions SmartParts/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public override void OnStart(StartState state)
initLight(true, "light-go");
}

void OnDestroy()
{
GameEvents.onVesselChange.Remove(onVesselChange);
}

public override void OnActive()
{
//If staging enabled, set timer
Expand Down
6 changes: 6 additions & 0 deletions SmartParts/Valve.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public override void OnStart(StartState state) {
}
}

void OnDestroy()
{
GameEvents.onVesselChange.Remove(onVesselChange);
}


public override void OnUpdate() {
if (isOpen) {
double receivedRessource = 0;
Expand Down
2 changes: 1 addition & 1 deletion deploy.bat
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ copy /Y %VERSIONFILE% %GAMEDATA%\%GAMEDIR%
xcopy /y /s /I %GAMEDATA%\%GAMEDIR% "%H%\GameData\%GAMEDIR%"
xcopy /y /s /I %GAMEDATA%\%GAMEDIR% "%DP0%\GameData\%GAMEDIR%"

pause
rem pause

0 comments on commit a03d7df

Please sign in to comment.