Fixed memory leak caused by submarine preview images, changed Submarine.SavedSubmarines to a property that prevents removing submarines from outside the class without disposing the preview image. Closes #498
This commit is contained in:
@@ -815,7 +815,7 @@ namespace Barotrauma.Networking
|
|||||||
string subName = inc.ReadString();
|
string subName = inc.ReadString();
|
||||||
string subHash = inc.ReadString();
|
string subHash = inc.ReadString();
|
||||||
|
|
||||||
var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == subName && s.MD5Hash.Hash == subHash);
|
var matchingSub = Submarine.SavedSubmarines.FirstOrDefault(s => s.Name == subName && s.MD5Hash.Hash == subHash);
|
||||||
if (matchingSub != null)
|
if (matchingSub != null)
|
||||||
{
|
{
|
||||||
submarines.Add(matchingSub);
|
submarines.Add(matchingSub);
|
||||||
@@ -1181,8 +1181,12 @@ namespace Barotrauma.Networking
|
|||||||
case FileTransferType.Submarine:
|
case FileTransferType.Submarine:
|
||||||
new GUIMessageBox("Download finished", "File \"" + transfer.FileName + "\" was downloaded succesfully.");
|
new GUIMessageBox("Download finished", "File \"" + transfer.FileName + "\" was downloaded succesfully.");
|
||||||
var newSub = new Submarine(transfer.FilePath);
|
var newSub = new Submarine(transfer.FilePath);
|
||||||
Submarine.SavedSubmarines.RemoveAll(s => s.Name == newSub.Name && s.MD5Hash.Hash == newSub.MD5Hash.Hash);
|
var existingSubs = Submarine.SavedSubmarines.Where(s => s.Name == newSub.Name && s.MD5Hash.Hash == newSub.MD5Hash.Hash).ToList();
|
||||||
Submarine.SavedSubmarines.Add(newSub);
|
foreach (Submarine existingSub in existingSubs)
|
||||||
|
{
|
||||||
|
existingSub.Dispose();
|
||||||
|
}
|
||||||
|
Submarine.AddToSavedSubs(newSub);
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++)
|
for (int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using Lidgren.Network;
|
using Lidgren.Network;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Barotrauma
|
namespace Barotrauma
|
||||||
{
|
{
|
||||||
@@ -151,7 +152,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
int votes = inc.ReadByte();
|
int votes = inc.ReadByte();
|
||||||
string subName = inc.ReadString();
|
string subName = inc.ReadString();
|
||||||
Submarine sub = Submarine.SavedSubmarines.Find(sm => sm.Name == subName);
|
Submarine sub = Submarine.SavedSubmarines.FirstOrDefault(sm => sm.Name == subName);
|
||||||
SetVoteText(GameMain.NetLobbyScreen.SubList, sub, votes);
|
SetVoteText(GameMain.NetLobbyScreen.SubList, sub, votes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ namespace Barotrauma
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (Submarine.SavedSubmarines.Count > 0) subList.Select(Submarine.SavedSubmarines[0]);
|
if (Submarine.SavedSubmarines.Any()) subList.Select(Submarine.SavedSubmarines.First());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateLoadMenu()
|
public void UpdateLoadMenu()
|
||||||
|
|||||||
@@ -782,8 +782,8 @@ namespace Barotrauma
|
|||||||
CanBeFocused = false
|
CanBeFocused = false
|
||||||
};
|
};
|
||||||
|
|
||||||
var matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == sub.Name && s.MD5Hash.Hash == sub.MD5Hash.Hash);
|
var matchingSub = Submarine.SavedSubmarines.FirstOrDefault(s => s.Name == sub.Name && s.MD5Hash.Hash == sub.MD5Hash.Hash);
|
||||||
if (matchingSub == null) matchingSub = Submarine.SavedSubmarines.Find(s => s.Name == sub.Name);
|
if (matchingSub == null) matchingSub = Submarine.SavedSubmarines.FirstOrDefault(s => s.Name == sub.Name);
|
||||||
|
|
||||||
if (matchingSub == null)
|
if (matchingSub == null)
|
||||||
{
|
{
|
||||||
@@ -1467,8 +1467,8 @@ namespace Barotrauma
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Submarine sub = Submarine.SavedSubmarines.Find(m => m.Name == subName && m.MD5Hash.Hash == md5Hash);
|
Submarine sub = Submarine.SavedSubmarines.FirstOrDefault(m => m.Name == subName && m.MD5Hash.Hash == md5Hash);
|
||||||
if (sub == null) sub = Submarine.SavedSubmarines.Find(m => m.Name == subName);
|
if (sub == null) sub = Submarine.SavedSubmarines.FirstOrDefault(m => m.Name == subName);
|
||||||
|
|
||||||
var matchingListSub = subList.children.Find(c => c.UserData == sub);
|
var matchingListSub = subList.children.Find(c => c.UserData == sub);
|
||||||
if (matchingListSub != null)
|
if (matchingListSub != null)
|
||||||
|
|||||||
@@ -254,10 +254,13 @@ namespace Barotrauma
|
|||||||
partial void DisposeTexture()
|
partial void DisposeTexture()
|
||||||
{
|
{
|
||||||
//check if another sprite is using the same texture
|
//check if another sprite is using the same texture
|
||||||
|
if (!string.IsNullOrEmpty(file)) //file can be empty if the sprite is created directly from a Texture2D instance
|
||||||
|
{
|
||||||
foreach (Sprite s in list)
|
foreach (Sprite s in list)
|
||||||
{
|
{
|
||||||
if (s.file == file) return;
|
if (s.file == file) return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//if not, free the texture
|
//if not, free the texture
|
||||||
if (texture != null)
|
if (texture != null)
|
||||||
@@ -267,6 +270,5 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (GameMain.Server.SubSelectionMode == SelectionMode.Random)
|
if (GameMain.Server.SubSelectionMode == SelectionMode.Random)
|
||||||
{
|
{
|
||||||
var nonShuttles = Submarine.SavedSubmarines.FindAll(c => !c.HasTag(SubmarineTag.Shuttle) && !c.HasTag(SubmarineTag.HideInMenus));
|
var nonShuttles = Submarine.SavedSubmarines.Where(c => !c.HasTag(SubmarineTag.Shuttle) && !c.HasTag(SubmarineTag.HideInMenus)).ToList();
|
||||||
SelectedSub = nonShuttles[Rand.Range(0, nonShuttles.Count)];
|
SelectedSub = nonShuttles[Rand.Range(0, nonShuttles.Count)];
|
||||||
}
|
}
|
||||||
if (GameMain.Server.ModeSelectionMode == SelectionMode.Random)
|
if (GameMain.Server.ModeSelectionMode == SelectionMode.Random)
|
||||||
|
|||||||
@@ -51,7 +51,11 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public static bool LockX, LockY;
|
public static bool LockX, LockY;
|
||||||
|
|
||||||
public static List<Submarine> SavedSubmarines = new List<Submarine>();
|
private static List<Submarine> savedSubmarines = new List<Submarine>();
|
||||||
|
public static IEnumerable<Submarine> SavedSubmarines
|
||||||
|
{
|
||||||
|
get { return savedSubmarines; }
|
||||||
|
}
|
||||||
|
|
||||||
public static readonly Vector2 GridSize = new Vector2(16.0f, 16.0f);
|
public static readonly Vector2 GridSize = new Vector2(16.0f, 16.0f);
|
||||||
|
|
||||||
@@ -875,9 +879,18 @@ namespace Barotrauma
|
|||||||
|
|
||||||
//saving/loading ----------------------------------------------------
|
//saving/loading ----------------------------------------------------
|
||||||
|
|
||||||
|
public static void AddToSavedSubs(Submarine sub)
|
||||||
|
{
|
||||||
|
savedSubmarines.Add(sub);
|
||||||
|
}
|
||||||
|
|
||||||
public static void RefreshSavedSubs()
|
public static void RefreshSavedSubs()
|
||||||
{
|
{
|
||||||
SavedSubmarines.Clear();
|
for (int i = savedSubmarines.Count - 1; i>= 0; i--)
|
||||||
|
{
|
||||||
|
savedSubmarines[i].Dispose();
|
||||||
|
}
|
||||||
|
System.Diagnostics.Debug.Assert(savedSubmarines.Count == 0);
|
||||||
|
|
||||||
if (!Directory.Exists(SavePath))
|
if (!Directory.Exists(SavePath))
|
||||||
{
|
{
|
||||||
@@ -921,7 +934,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
foreach (string path in filePaths)
|
foreach (string path in filePaths)
|
||||||
{
|
{
|
||||||
SavedSubmarines.Add(new Submarine(path));
|
savedSubmarines.Add(new Submarine(path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1285,6 +1298,15 @@ namespace Barotrauma
|
|||||||
DockedTo.Clear();
|
DockedTo.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
savedSubmarines.Remove(this);
|
||||||
|
#if CLIENT
|
||||||
|
PreviewImage.Remove();
|
||||||
|
PreviewImage = null;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
||||||
{
|
{
|
||||||
msg.Write(ID);
|
msg.Write(ID);
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ namespace Barotrauma.Networking
|
|||||||
case (byte)FileTransferType.Submarine:
|
case (byte)FileTransferType.Submarine:
|
||||||
string fileName = inc.ReadString();
|
string fileName = inc.ReadString();
|
||||||
string fileHash = inc.ReadString();
|
string fileHash = inc.ReadString();
|
||||||
var requestedSubmarine = Submarine.SavedSubmarines.Find(s => s.Name == fileName && s.MD5Hash.Hash == fileHash);
|
var requestedSubmarine = Submarine.SavedSubmarines.FirstOrDefault(s => s.Name == fileName && s.MD5Hash.Hash == fileHash);
|
||||||
|
|
||||||
if (requestedSubmarine != null)
|
if (requestedSubmarine != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
case VoteType.Sub:
|
case VoteType.Sub:
|
||||||
string subName = inc.ReadString();
|
string subName = inc.ReadString();
|
||||||
Submarine sub = Submarine.SavedSubmarines.Find(s => s.Name == subName);
|
Submarine sub = Submarine.SavedSubmarines.FirstOrDefault(s => s.Name == subName);
|
||||||
sender.SetVote(voteType, sub);
|
sender.SetVote(voteType, sub);
|
||||||
#if CLIENT
|
#if CLIENT
|
||||||
UpdateVoteTexts(GameMain.Server.ConnectedClients, voteType);
|
UpdateVoteTexts(GameMain.Server.ConnectedClients, voteType);
|
||||||
|
|||||||
Reference in New Issue
Block a user