Browse Source

Memory leak repaired (a bit)

-Added UndoStack limit to 25 (temporarily)
-Repaired total memory leak, it is still, but less (gotta repair that soon)
flabbet 6 years ago
parent
commit
5fce2ae4b3

+ 48 - 0
PixiEditor/Models/StackEx.cs

@@ -0,0 +1,48 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Models
+{
+    public class StackEx<T>
+    {
+        public int Count
+        {
+            get { return items.Count; }
+        }
+
+        private List<T> items = new List<T>();
+
+        public void Clear()
+        {
+            items.Clear();
+        }
+
+        public T Peek()
+        {
+            return items[items.Count - 1];
+        }
+
+        public void Push(T item)
+        {
+            items.Add(item);
+        }
+        public T Pop()
+        {
+            if (items.Count > 0)
+            {
+                T temp = items[items.Count - 1];
+                items.RemoveAt(items.Count - 1);
+                return temp;
+            }
+            else
+                return default(T);
+        }
+        public void Remove(int itemAtPosition)
+        {
+            items.RemoveAt(itemAtPosition);
+        }
+    }
+}

+ 19 - 4
PixiEditor/Models/Tools/ToolSet.cs

@@ -18,14 +18,27 @@ namespace PixiEditor.Models.Tools
         private Coordinates _activeCoordinates = new Coordinates();
         private bool _toolIsExecuting = false;
         private int _asyncDelay = 15;
+        private WriteableBitmap _oldBitmap;
+        private Image _oldImage;
 
+        /// <summary>
+        /// Executes tool action
+        /// </summary>
+        /// <param name="layer">Layer to operate on.</param>
+        /// <param name="startingCoords">Click coordinates.</param>
+        /// <param name="color">Color that tool will use.</param>
+        /// <param name="toolSize">Size/thickness of tool</param>
+        /// <param name="tool">Tool to execute</param>
+        /// <returns></returns>
         public Layer ExecuteTool(Layer layer, Coordinates startingCoords, Color color,int toolSize, ToolType tool)
         {
             if (toolSize < 1) return null;
             Layer cLayer = layer;
-            WriteableBitmap oldBitmap = layer.LayerBitmap.Clone();
-            Image oldImage = layer.LayerImage;
-            oldImage.Source = oldBitmap;
+
+            _oldBitmap = new WriteableBitmap(layer.LayerBitmap);
+            _oldImage = layer.LayerImage;
+            _oldImage.Source = _oldBitmap;
+
             switch (tool)
             {
                 case ToolType.Pen:
@@ -70,10 +83,12 @@ namespace PixiEditor.Models.Tools
             }
             if (tool != ToolType.ColorPicker)
             {
-                UndoManager.RecordChanges("ActiveLayer", new Layer(oldBitmap, oldImage), cLayer, string.Format("{0} Tool.", tool.ToString()));
+                UndoManager.RecordChanges("ActiveLayer", new Layer(_oldBitmap, _oldImage), cLayer, string.Format("{0} Tool.", tool.ToString()));
             }
+
             return cLayer;
         }
+
         /// <summary>
         /// Not working yet.
         /// </summary>

+ 14 - 6
PixiEditor/Models/UndoManager.cs

@@ -14,10 +14,11 @@ namespace PixiEditor.Models
 {
     public static class UndoManager
     {
-        public static Stack<Change> UndoStack { get; set; } = new Stack<Change>(); 
-        public static Stack<Change> RedoStack { get; set; } = new Stack<Change>();
+        public static StackEx<Change> UndoStack { get; set; } = new StackEx<Change>(); 
+        public static StackEx<Change> RedoStack { get; set; } = new StackEx<Change>();
         private static bool _stopRecording = false; 
         private static List<Change> _recordedChanges = new List<Change>();
+        private static int _stackLimit = 25;
         private static bool _lastChangeWasUndo = false;
         public static bool CanUndo
         {
@@ -56,7 +57,10 @@ namespace PixiEditor.Models
         {
             if (_stopRecording == false)
             {
-                _recordedChanges.Add(new Change(property, oldValue, newValue, undoDescription));
+                if (_recordedChanges.Count < 3)
+                {
+                    _recordedChanges.Add(new Change(property, oldValue, newValue, undoDescription));
+                }
             }
         }
 
@@ -83,13 +87,17 @@ namespace PixiEditor.Models
         /// <param name="undoDescription">Description of change.</param>
         public static void AddUndoChange(string property, object oldValue, object newValue, string undoDescription = null)
         {
-            if(_lastChangeWasUndo == false && RedoStack.Count > 0)
+            if(_lastChangeWasUndo == false && RedoStack.Count > 0) //Cleares RedoStack if las move wasn't redo or undo and if redo stack is greater than 0
             {
                 RedoStack.Clear();
             }
             _lastChangeWasUndo = false;
-            UndoStack.Push(new Change(property, oldValue, newValue, undoDescription));
-
+            UndoStack.Push(new Change(property, oldValue, newValue, undoDescription)); //Adds change to UndoStack
+            if(UndoStack.Count > _stackLimit) //If Undostack hits limit, removes elements from beggining of stack
+            {
+                UndoStack.Remove(0);
+            }
+            Debug.WriteLine(UndoStack.Count + " " + RedoStack.Count);
         }
         /// <summary>
         /// Sets top property in UndoStack to Old Value

+ 1 - 0
PixiEditor/PixiEditor.csproj

@@ -99,6 +99,7 @@
     <Compile Include="Models\MousePositionConverter.cs" />
     <Compile Include="Models\NewFileDialog.cs" />
     <Compile Include="Models\ExportFileDialog.cs" />
+    <Compile Include="Models\StackEx.cs" />
     <Compile Include="Models\Tools\ToolSet.cs" />
     <Compile Include="Models\ToolManager.cs" />
     <Compile Include="Models\Tools\ToolType.cs" />

+ 20 - 0
PixiEditorTests/Models/UndoManagerTests.cs

@@ -0,0 +1,20 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using PixiEditor.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PixiEditor.Models.Tests
+{
+    [TestClass()]
+    public class UndoManagerTests
+    {
+        [TestMethod()]
+        public void StopRecordingTest()
+        {
+            Assert.Fail();
+        }
+    }
+}

+ 106 - 0
PixiEditorTests/PixiEditorTests.csproj

@@ -0,0 +1,106 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{2725573E-D3CD-44FF-8331-8527A0A2ADE2}</ProjectGuid>
+    <OutputType>Library</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>PixiEditorTests</RootNamespace>
+    <AssemblyName>PixiEditorTests</AssemblyName>
+    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
+    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
+    <IsCodedUITest>False</IsCodedUITest>
+    <TestProjectType>UnitTest</TestProjectType>
+    <TargetFrameworkProfile />
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <HintPath>..\PixiEditor\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
+    </Reference>
+    <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <HintPath>..\PixiEditor\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
+    </Reference>
+    <Reference Include="System" />
+  </ItemGroup>
+  <Choose>
+    <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
+      <ItemGroup>
+        <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
+      </ItemGroup>
+    </When>
+    <Otherwise />
+  </Choose>
+  <ItemGroup>
+    <Compile Include="Models\UndoManagerTests.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\PixiEditor\PixiEditor.csproj">
+      <Project>{04324995-2B67-402A-A427-F35AFA014D05}</Project>
+      <Name>PixiEditor</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Choose>
+    <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
+      <ItemGroup>
+        <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+          <Private>False</Private>
+        </Reference>
+        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+          <Private>False</Private>
+        </Reference>
+        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+          <Private>False</Private>
+        </Reference>
+        <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+          <Private>False</Private>
+        </Reference>
+      </ItemGroup>
+    </When>
+  </Choose>
+  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
+    <Error Condition="!Exists('..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
+  </Target>
+  <Import Project="..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\PixiEditor\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 36 - 0
PixiEditorTests/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("PixiEditorTests")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("PixiEditorTests")]
+[assembly: AssemblyCopyright("Copyright ©  2018")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("2725573e-d3cd-44ff-8331-8527a0a2ade2")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 5 - 0
PixiEditorTests/packages.config

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net461" />
+  <package id="MSTest.TestFramework" version="1.3.2" targetFramework="net461" />
+</packages>