From 579140404be5c3fcec0d005e9b5ba36dea13f918 Mon Sep 17 00:00:00 2001 From: Alexander Marek Date: Fri, 18 Nov 2016 14:08:26 +0100 Subject: [PATCH] #148 fixed sigsegv crashes --- .../MvxAndroidLocalFileImageLoader.cs | 34 +++++++++---------- .../MvxDynamicImageHelper.cs | 9 ++++- .../MvxFileDownloadCache.cs | 2 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/DownloadCache/MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs b/DownloadCache/MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs index a509fe2d..b0fed110 100644 --- a/DownloadCache/MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs +++ b/DownloadCache/MvvmCross.Plugins.DownloadCache.Droid/MvxAndroidLocalFileImageLoader.cs @@ -23,8 +23,8 @@ public class MvxAndroidLocalFileImageLoader { private const string ResourcePrefix = "res:"; - private readonly IDictionary> _memCache = - new Dictionary>(); + //private readonly IDictionary> _memCache = + // new Dictionary>(); public async Task> Load(string localPath, bool shouldCache, int maxWidth, int maxHeight) { @@ -166,20 +166,20 @@ private static int CalculateInSampleSize(BitmapFactory.Options options, int reqW private bool TryGetCachedBitmap(string localPath, int maxWidth, int maxHeight, out Bitmap bitmap) { - var key = new CacheKey(localPath, maxWidth, maxHeight); - WeakReference reference; + //var key = new CacheKey(localPath, maxWidth, maxHeight); + //WeakReference reference; - if (_memCache.TryGetValue(key, out reference)) - { - Bitmap target; - if (reference.TryGetTarget(out target) && target != null && target.Handle != IntPtr.Zero && !target.IsRecycled) - { - bitmap = target; - return true; - } + //if (_memCache.TryGetValue(key, out reference)) + //{ + // Bitmap target; + // if (reference.TryGetTarget(out target) && target != null && target.Handle != IntPtr.Zero && !target.IsRecycled) + // { + // bitmap = target; + // return true; + // } - _memCache.Remove(key); - } + // _memCache.Remove(key); + //} bitmap = null; return false; @@ -187,10 +187,10 @@ private bool TryGetCachedBitmap(string localPath, int maxWidth, int maxHeight, o private void AddToCache(string localPath, int maxWidth, int maxHeight, Bitmap bitmap) { - if (bitmap == null) return; + //if (bitmap == null) return; - var key = new CacheKey(localPath, maxWidth, maxHeight); - _memCache[key] = new WeakReference(bitmap); + //var key = new CacheKey(localPath, maxWidth, maxHeight); + //_memCache[key] = new WeakReference(bitmap); } private class CacheKey diff --git a/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxDynamicImageHelper.cs b/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxDynamicImageHelper.cs index 2410326d..da99e9e3 100644 --- a/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxDynamicImageHelper.cs +++ b/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxDynamicImageHelper.cs @@ -20,6 +20,8 @@ public class MvxDynamicImageHelper : IMvxImageHelper where T : class { + private readonly IMvxMainThreadDispatcher _dispatcher; + #region ImageState enum public enum ImageState @@ -31,6 +33,11 @@ public enum ImageState #endregion ImageState enum + public MvxDynamicImageHelper(IMvxMainThreadDispatcher dispatcher) + { + _dispatcher = dispatcher; + } + private ImageState _currentImageState = ImageState.DefaultShown; private CancellationTokenSource _cancellationSource; @@ -103,7 +110,7 @@ public void Dispose() private void FireImageChanged(T image) { var handler = ImageChanged; - handler?.Invoke(this, new MvxValueEventArgs(image)); + _dispatcher.RequestMainThreadAction(() => handler?.Invoke(this, new MvxValueEventArgs(image))); } private async Task RequestImageAsync(string imageSource) diff --git a/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxFileDownloadCache.cs b/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxFileDownloadCache.cs index 8a78f99f..de8a3976 100644 --- a/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxFileDownloadCache.cs +++ b/DownloadCache/MvvmCross.Plugins.DownloadCache/MvxFileDownloadCache.cs @@ -402,7 +402,7 @@ public Timer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan pe { if (IsCancellationRequested) break; - Task.Run(() => tuple.Item1(tuple.Item2)); + await Task.Run(() => tuple.Item1(tuple.Item2)); await Task.Delay(period); } }, Tuple.Create(callback, state), CancellationToken.None,