From 0a6bcb2d8277a7be57e994bc6fa5cfd449aef4d3 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Sat, 14 Feb 2015 00:28:29 -0800 Subject: [PATCH] Fixed possible NullReferenceException points for asynchronous methods. --- Combot/Bot.cs | 2 +- IRCServices/Messaging/Messages.cs | 5 ++--- Interface/ViewModels/MainViewModel.cs | 10 ++++++---- Modules/Weather Information/Weather_Information.cs | 9 ++++++--- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Combot/Bot.cs b/Combot/Bot.cs index 13288fb..44ecf26 100755 --- a/Combot/Bot.cs +++ b/Combot/Bot.cs @@ -505,7 +505,7 @@ namespace Combot Regex whoRegex = new Regex(whoStyle); IRC.Command.SendWho(newCommand.Nick.Nickname); ServerReplyMessage whoReply = IRC.Message.GetServerReply(IRCReplyCode.RPL_WHOREPLY, whoStyle); - if (whoReply.ReplyCode != 0) + if (whoReply != null && whoReply.ReplyCode != 0) { Match whoMatch = whoRegex.Match(whoReply.Message); diff --git a/IRCServices/Messaging/Messages.cs b/IRCServices/Messaging/Messages.cs index 45483df..8ecae29 100755 --- a/IRCServices/Messaging/Messages.cs +++ b/IRCServices/Messaging/Messages.cs @@ -445,11 +445,12 @@ namespace Combot.IRCServices.Messaging } } + string rawMessage = message; await Task.Run(() => { if (RawMessageEvent != null) { - RawMessageEvent(this, message); + RawMessageEvent(this, rawMessage); } }); } @@ -462,7 +463,6 @@ namespace Combot.IRCServices.Messaging reply.Match = match; ServerReplyEvent += (sender, e) => HandleReply(sender, e, reply); reply.Ready.Wait(TimeSpan.FromMilliseconds(5000)); - ServerReplyEvent -= (obj, e) => HandleReply(obj, e, reply); return reply.Result; } @@ -473,7 +473,6 @@ namespace Combot.IRCServices.Messaging error.Match = match; ServerReplyEvent += (sender, e) => HandleError(sender, e, error); error.Ready.Wait(TimeSpan.FromMilliseconds(5000)); - ServerReplyEvent -= (sender, e) => HandleError(sender, e, error); return error.Result; } diff --git a/Interface/ViewModels/MainViewModel.cs b/Interface/ViewModels/MainViewModel.cs index 56e0f86..872c7fd 100755 --- a/Interface/ViewModels/MainViewModel.cs +++ b/Interface/ViewModels/MainViewModel.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using System.Net; using System.Runtime.Remoting.Channels; using System.Threading; +using System.Windows; using System.Windows.Documents; using Combot; using Combot.IRCServices.Messaging; @@ -385,7 +386,7 @@ namespace Interface.ViewModels } if (LocationList.Contains(location)) { - App.Current.Dispatcher.Invoke((Action)(() => LocationList.Remove(location))); + Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Remove(location))); } BufferLock.EnterWriteLock(); if (BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == location)) @@ -445,7 +446,7 @@ namespace Interface.ViewModels BufferLock.ExitWriteLock(); if (SelectedServer == server && !LocationList.Contains(location)) { - App.Current.Dispatcher.Invoke((Action) (() => LocationList.Add(location))); + Application.Current.Dispatcher.Invoke((Action) (() => LocationList.Add(location))); } BufferLock.EnterWriteLock(); BufferInfo buffer = BufferList.Find(buf => buf.Server == server && buf.Location == location); @@ -460,12 +461,13 @@ namespace Interface.ViewModels private void ChangeServer() { - App.Current.Dispatcher.Invoke((Action)(() => LocationList.Clear())); + Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Clear())); for (int i = 0; i < BufferList.Count; i++) { if (BufferList[i].Server == SelectedServer) { - App.Current.Dispatcher.Invoke((Action)(() => LocationList.Add(BufferList[i].Location))); + int index = i; + Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Add(BufferList[index].Location))); } } if (LocationList.Any()) diff --git a/Modules/Weather Information/Weather_Information.cs b/Modules/Weather Information/Weather_Information.cs index 62e72cf..6cbc957 100755 --- a/Modules/Weather Information/Weather_Information.cs +++ b/Modules/Weather Information/Weather_Information.cs @@ -43,14 +43,17 @@ namespace Combot.Modules.Plugins XmlNodeList nodes2 = doc2.SelectNodes("/current_observation"); string location = ""; - if (nodes2.Count > 0) + if (nodes2 != null && nodes2.Count > 0) { foreach (XmlNode node2 in nodes2) { XmlNodeList sub_node2 = doc2.SelectNodes("/current_observation/display_location"); - foreach (XmlNode xn2 in sub_node2) + if (sub_node2 != null) { - location = xn2["full"].InnerText; + foreach (XmlNode xn2 in sub_node2) + { + location = xn2["full"].InnerText; + } } } }