comparison ServerMonitor/Objects/ServerMonitor.cs @ 36:10e60b05c7ec

Fix memory leak each time a check was executed.
author Brad Greco <brad@bgreco.net>
date Sun, 15 Sep 2019 20:48:55 -0400
parents 2ffb0bda7705
children 8ab98a803d39
comparison
equal deleted inserted replaced
35:2ffb0bda7705 36:10e60b05c7ec
33 private bool running, networkAvailable, suspend; 33 private bool running, networkAvailable, suspend;
34 // List of check execution tasks that have been started. 34 // List of check execution tasks that have been started.
35 // A check task begins by sleeping until the next scheduled execution time, 35 // A check task begins by sleeping until the next scheduled execution time,
36 // then executes. 36 // then executes.
37 private Dictionary<Task<CheckResult>, int> tasks; 37 private Dictionary<Task<CheckResult>, int> tasks;
38 // XML serializer that can handle servers and all check types.
39 private XmlSerializer xmlSerializer;
38 private ServerSummaryForm mainForm; 40 private ServerSummaryForm mainForm;
39 private WaveOut waveOut = new WaveOut(); 41 private WaveOut waveOut = new WaveOut();
40 MediaFoundationReader mediaReader; 42 MediaFoundationReader mediaReader;
41 43
42 /// <summary>Fires when the status of a check changes.</summary> 44 /// <summary>Fires when the status of a check changes.</summary>
61 this.mainForm = mainForm; 63 this.mainForm = mainForm;
62 // Store configuration in %appdata%\ServerMonitor 64 // Store configuration in %appdata%\ServerMonitor
63 configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ServerMonitor"); 65 configFileDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ServerMonitor");
64 ConfigFile = Path.Combine(configFileDir, "servers.xml"); 66 ConfigFile = Path.Combine(configFileDir, "servers.xml");
65 logger = new Logger(Path.Combine(configFileDir, "monitor.log")); 67 logger = new Logger(Path.Combine(configFileDir, "monitor.log"));
68 xmlSerializer = new XmlSerializer(typeof(List<Server>), Check.CheckTypes);
66 } 69 }
67 70
68 /// <summary>Registers a new server with the server monitor.</summary> 71 /// <summary>Registers a new server with the server monitor.</summary>
69 /// <param name="server">The server to be added.</param> 72 /// <param name="server">The server to be added.</param>
70 public void AddServer(Server server) 73 public void AddServer(Server server)
97 Read: 100 Read:
98 TextReader reader = null; 101 TextReader reader = null;
99 try 102 try
100 { 103 {
101 reader = new StreamReader(ConfigFile); 104 reader = new StreamReader(ConfigFile);
102 XmlSerializer serializer = CreateXmlSerializer();
103 Servers.Clear(); 105 Servers.Clear();
104 Servers.AddRange((List<Server>)serializer.Deserialize(reader)); 106 Servers.AddRange((List<Server>)xmlSerializer.Deserialize(reader));
105 // Do some more set-up now that the servers and checks have been loaded. 107 // Do some more set-up now that the servers and checks have been loaded.
106 foreach (Server server in Servers) 108 foreach (Server server in Servers)
107 { 109 {
108 // Read private keys into memory if they are accessible and not encrypted. 110 // Read private keys into memory if they are accessible and not encrypted.
109 // If PrivateKeyFile != null, it means same the key has already been loaded for 111 // If PrivateKeyFile != null, it means same the key has already been loaded for
167 /// <summary>Saves all servers and checks to the config file.</summary> 169 /// <summary>Saves all servers and checks to the config file.</summary>
168 public void SaveServers() 170 public void SaveServers()
169 { 171 {
170 GenerateIds(); 172 GenerateIds();
171 TextWriter writer = null; 173 TextWriter writer = null;
172 XmlSerializer serializer = null;
173 try 174 try
174 { 175 {
175 // Make a backup first in case something goes wrong in the middle of writing. 176 // Make a backup first in case something goes wrong in the middle of writing.
176 File.Copy(ConfigFile, ConfigFile + ".bak", true); 177 File.Copy(ConfigFile, ConfigFile + ".bak", true);
177 } 178 }
178 catch { } 179 catch { }
179 try 180 try
180 { 181 {
181 writer = new StreamWriter(ConfigFile); 182 writer = new StreamWriter(ConfigFile);
182 serializer = CreateXmlSerializer(); 183 xmlSerializer.Serialize(writer, Servers);
183 serializer.Serialize(writer, Servers);
184 } 184 }
185 catch (DirectoryNotFoundException) 185 catch (DirectoryNotFoundException)
186 { 186 {
187 // If the directory does not exist, create it and try again. 187 // If the directory does not exist, create it and try again.
188 Directory.CreateDirectory(configFileDir); 188 Directory.CreateDirectory(configFileDir);
189 writer = new StreamWriter(ConfigFile); 189 writer = new StreamWriter(ConfigFile);
190 serializer = CreateXmlSerializer(); 190 xmlSerializer.Serialize(writer, Servers);
191 serializer.Serialize(writer, Servers);
192 } 191 }
193 finally 192 finally
194 { 193 {
195 writer?.Close(); 194 writer?.Close();
196 } 195 }