Skip to content

Commit

Permalink
Merge pull request #12 from Mine02C4/bufix/issue11
Browse files Browse the repository at this point in the history
OData ライブラリ内でのメモリリークを回避するための可能な限りの修正
  • Loading branch information
Mine02C4 authored Apr 2, 2018
2 parents 4c99473 + 236f425 commit a286ca7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 24 deletions.
30 changes: 25 additions & 5 deletions HiyoshiCfhClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public Client(string tokenType, string accessToken, DebugConsole debugConsole)
_DebugConsole = debugConsole;
}

private void ResetContext()
internal void ResetContext()
{
Context = new Container(new Uri("https://hiyoshicfhweb.azurewebsites.net/odata"));
GC.Collect();
Context.MergeOption = MergeOption.PreserveChanges;
if (TokenType != null && AccessToken != null)
{
Context.SendingRequest2 += (sender, eventArgs) =>
Expand Down Expand Up @@ -104,6 +106,7 @@ void InitAdmiral()
try
{
Context.SaveChanges();
DetachAll();
}
catch (DataServiceRequestException ex)
{
Expand Down Expand Up @@ -153,6 +156,7 @@ void UpdateAdmiral()
try
{
Context.SaveChanges();
DetachAll();
}
catch (DataServiceRequestException ex)
{
Expand Down Expand Up @@ -188,6 +192,7 @@ void UpdateShipTypes()
}
}
Context.SaveChanges();
DetachAll();
}
catch (Exception ex)
{
Expand All @@ -205,6 +210,7 @@ void UpdateShipInfoes()
(x, y) => x.Id == y.ShipInfoId, x => new WebShipInfo(x),
x => Context.AddToShipInfoes(x));
Context.SaveChanges();
DetachAll();
}
catch (Exception ex)
{
Expand All @@ -222,6 +228,7 @@ void UpdateSlotItemInfoes()
(x, y) => x.Id == y.SlotItemInfoId, x => new WebSlotItemInfo(x),
x => Context.AddToSlotItemInfoes(x));
Context.SaveChanges();
DetachAll();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -251,6 +258,7 @@ await factory.StartNew(() =>
);
OutDebugConsole("Saving ship data");
Context.SaveChanges();
DetachAll();
OutDebugConsole("Saved ship data");
}
catch (DataServiceRequestException ex)
Expand Down Expand Up @@ -289,6 +297,7 @@ await factory.StartNew(() =>
);
OutDebugConsole("Saving SlotItem data");
Context.SaveChanges();
DetachAll();
OutDebugConsole("Saved SlotItem data");
}
catch (DataServiceRequestException ex)
Expand Down Expand Up @@ -325,11 +334,8 @@ await factory.StartNew(() =>
);
OutDebugConsole("Saving quest data");
Context.SaveChanges();
DetachAll();
OutDebugConsole("Saved quest data");
foreach (var quest in quests)
{
Context.Detach(quest);
}
}
catch (DataServiceRequestException ex)
{
Expand All @@ -354,6 +360,7 @@ await factory.StartNew(() =>
record.AdmiralId = Admiral.AdmiralId;
Context.AddToMaterialRecords(record);
Context.SaveChanges();
DetachAll();
}
catch (DataServiceRequestException ex)
{
Expand Down Expand Up @@ -418,6 +425,19 @@ void SyncWithOData<T, U>(IEnumerable<T> localList, IEnumerable<U> odataList,
}
}
}

void DetachAll()
{
foreach (var entity in Context.Entities.ToList())
{
Context.Detach(entity.Entity);
}

foreach (var link in Context.Links.ToList())
{
Context.DetachLink(link.Source, link.SourceProperty, link.Target);
}
}
}

#region 例外クラス
Expand Down
16 changes: 8 additions & 8 deletions HiyoshiCfhClient/HiyoshiCfhClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@
<Reference Include="Microsoft.Expression.Interactions, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\LivetCask.1.3.1.0\lib\net45\Microsoft.Expression.Interactions.dll</HintPath>
</Reference>
<Reference Include="Microsoft.OData.Client, Version=7.4.1.20214, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Client.7.4.1\lib\net45\Microsoft.OData.Client.dll</HintPath>
<Reference Include="Microsoft.OData.Client, Version=7.4.3.20321, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Client.7.4.3\lib\net45\Microsoft.OData.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.OData.Core, Version=7.4.1.20214, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Core.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll</HintPath>
<Reference Include="Microsoft.OData.Core, Version=7.4.3.20321, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Core.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.OData.Edm, Version=7.4.1.20214, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Edm.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll</HintPath>
<Reference Include="Microsoft.OData.Edm, Version=7.4.3.20321, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.OData.Edm.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Spatial, Version=7.4.1.20214, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Spatial.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll</HintPath>
<Reference Include="Microsoft.Spatial, Version=7.4.3.20321, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Spatial.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll</HintPath>
</Reference>
<Reference Include="Nekoxy, Version=1.5.3.21, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nekoxy.1.5.3.21\lib\net45\Nekoxy.dll</HintPath>
Expand Down
40 changes: 35 additions & 5 deletions HiyoshiCfhClient/ViewModels/ClientViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using HiyoshiCfhClient.Models;
using HiyoshiCfhClient.Utils;
using Livet;
using Livet.EventListeners;
using Livet.Messaging;
using StatefulModel.EventListeners;
using System;
using System.IO;
using System.Reactive.Linq;
Expand Down Expand Up @@ -90,6 +90,7 @@ public bool EnableAutoUpdate
PropertyChangedEventListener ItemyardListener = null;
Client Client = null;
QuestsTracker QuestsTracker;
IDisposable QuestSubscription;

public ClientViewModel()
{
Expand All @@ -100,7 +101,7 @@ public ClientViewModel()
TokenType = Settings.Current.TokenType;
EnableAutoUpdate = true;
var kccListener = new PropertyChangedEventListener(KanColleClient.Current);
kccListener.RegisterHandler(() => KanColleClient.Current.IsStarted, (_, __) =>
kccListener.RegisterHandler("IsStarted", (_, __) =>
{
if (!IsInited && KanColleClient.Current.IsStarted)
{
Expand All @@ -114,9 +115,10 @@ void InitHandlers()
{
if (OrganizationListener == null || ItemyardListener == null)
{
OutDebugConsole("InitHandlers");
#region 艦娘の変更検知
OrganizationListener = new PropertyChangedEventListener(KanColleClient.Current.Homeport.Organization);
OrganizationListener.RegisterHandler(() => KanColleClient.Current.Homeport.Organization.Ships,
OrganizationListener.RegisterHandler("Ships",
async (s, h) =>
{
try
Expand All @@ -143,7 +145,7 @@ void InitHandlers()
#endregion
#region 装備の変更検知
ItemyardListener = new PropertyChangedEventListener(KanColleClient.Current.Homeport.Itemyard);
ItemyardListener.RegisterHandler(() => KanColleClient.Current.Homeport.Itemyard.SlotItems,
ItemyardListener.RegisterHandler("SlotItems",
async (s, h) =>
{
try
Expand All @@ -164,7 +166,7 @@ void InitHandlers()
#endregion
#region 任務の取得検知
var proxy = KanColleClient.Current.Proxy;
proxy.api_get_member_questlist
QuestSubscription = proxy.api_get_member_questlist
.Select(QuestsTracker.QuestListSerialize)
.Where(x => x != null && x.api_count >= 0)
.Subscribe(async x => { await this.HandleQuests(x); });
Expand All @@ -176,6 +178,19 @@ void InitHandlers()
}
}

public void ResetHandlers()
{
OutDebugConsole("ResetHandlers");
OrganizationListener.Dispose();
OrganizationListener = null;
ItemyardListener.Dispose();
ItemyardListener = null;
KanColleClient.Current.Homeport.Materials.PropertyChanged -= MaterialsChanged;
QuestSubscription.Dispose();
IsInited = false;
InitHandlers();
}

private async void MaterialsChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var materials = sender as Materials;
Expand Down Expand Up @@ -339,6 +354,21 @@ async Task PrepareClient()
}
}

public void ResetClient()
{
OutDebugConsole("ResetClient");
Client = null;
PrepareClient();
}

public void ResetContext()
{
if (Client != null)
{
Client.ResetContext();
}
}

void InitClient()
{
if (CheckToken())
Expand Down
25 changes: 23 additions & 2 deletions HiyoshiCfhClient/Views/ClientView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,41 @@
<TextBlock Text="{Binding AccessToken}" VerticalAlignment="Center" Margin="0" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="ClearConsoleButton" Content="コンソールのクリア" Margin="10,0">
<Button x:Name="ClearConsoleButton" Content="コンソールのクリア" Margin="10,0,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="ClearConsole"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="WrackTokenButton" Content="トークンを破壊">
<Button x:Name="WrackTokenButton" Content="トークンを破壊" Margin="10,0,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="WrackToken"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="ResetHandlersButton" Content="Reset handler" Margin="10,0,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="ResetHandlers"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="ResetClientButton" Content="Reset client" Margin="10,0,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="ResetClient"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<Button x:Name="ResetContextButton" Content="Reset context" Margin="10,0,0,0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="ResetContext"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</StackPanel>
<TextBox x:Name="DebugConsole" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap" Text="{Binding DebugConsole}" Grid.Row="2" Background="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" Margin="10" Foreground="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
Expand Down
8 changes: 4 additions & 4 deletions HiyoshiCfhClient/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<package id="MetroRadiance.Core" version="2.2.1" targetFramework="net46" />
<package id="MetroTrilithon" version="0.2.0" targetFramework="net46" />
<package id="MetroTrilithon.Desktop" version="0.2.3" targetFramework="net46" />
<package id="Microsoft.OData.Client" version="7.4.1" targetFramework="net46" />
<package id="Microsoft.OData.Core" version="7.4.1" targetFramework="net46" />
<package id="Microsoft.OData.Edm" version="7.4.1" targetFramework="net46" />
<package id="Microsoft.Spatial" version="7.4.1" targetFramework="net46" />
<package id="Microsoft.OData.Client" version="7.4.3" targetFramework="net46" />
<package id="Microsoft.OData.Core" version="7.4.3" targetFramework="net46" />
<package id="Microsoft.OData.Edm" version="7.4.3" targetFramework="net46" />
<package id="Microsoft.Spatial" version="7.4.3" targetFramework="net46" />
<package id="Nekoxy" version="1.5.3.21" targetFramework="net46" />
<package id="Rx-Core" version="2.2.5" targetFramework="net46" />
<package id="Rx-Interfaces" version="2.2.5" targetFramework="net46" />
Expand Down

0 comments on commit a286ca7

Please sign in to comment.