Skip to content

Commit

Permalink
Merge pull request #12 from JuGGerNaunT/fix_issue11
Browse files Browse the repository at this point in the history
fixed AccessViolationException crash when switching between gm1 items
  • Loading branch information
PodeCaradox authored Apr 24, 2020
2 parents fcab4be + 4380f7d commit b0744f9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
14 changes: 13 additions & 1 deletion Gm1KonverterCrossPlatform/Files/DecodedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Files.Gm1Converter
{
class DecodedFile
class DecodedFile : IDisposable
{

#region Variables
Expand Down Expand Up @@ -147,6 +147,17 @@ public byte[] GetNewGM1Bytes()
return newFile.ToArray();
}

public void Dispose()
{
if (tilesImage != null)
{
foreach (TilesImage image in tilesImage)
{
image.Dispose();
}
}
}

/// <summary>
/// Creates the IMG from the byte array, handles Animation,Interfaces,TGXConstSize, Font
/// </summary>
Expand Down Expand Up @@ -232,6 +243,7 @@ private void CreateImgHeader(byte[] array)
private void CreateTileImage(byte[] array)
{
if (Logger.Loggeractiv) Logger.Log("CreateTileImage");
Dispose();
CreateOffsetAndSizeInByteArrayList(array);
CreateImgHeader(array);

Expand Down
12 changes: 11 additions & 1 deletion Gm1KonverterCrossPlatform/Files/TilesImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Gm1KonverterCrossPlatform.Files
{
class TilesImage
class TilesImage : IDisposable
{

#region Public
Expand Down Expand Up @@ -51,6 +51,14 @@ public TilesImage(int width, int height)

#region Methods

public void Dispose()
{
if (bmp != null)
{
bmp.Dispose();
}
}

/// <summary>
/// Add the Diamond Tile Img to the bigger Img
/// </summary>
Expand Down Expand Up @@ -179,6 +187,8 @@ internal void AddImgTileOnTopToImg(byte[] imgFileAsBytearray, int offsetX, int o
/// </summary>
internal unsafe void CreateImagefromList()
{
Dispose();

if (minusHeight == 9999999)
{
//is used for the correct height of the bitmap
Expand Down
22 changes: 21 additions & 1 deletion Gm1KonverterCrossPlatform/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Gm1KonverterCrossPlatform.ViewModels
{
public class MainWindowViewModel : ViewModelBase
public class MainWindowViewModel : ViewModelBase, IDisposable
{

#region Variables
Expand Down Expand Up @@ -383,6 +383,25 @@ internal ObservableCollection<Image> TGXImages

#region Methods

~MainWindowViewModel()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (File != null)
{
File.Dispose();
}
}

/// <summary>
/// Load the GM1 Files from the CrusaderPath
/// </summary>
Expand Down Expand Up @@ -434,6 +453,7 @@ internal bool DecodeData(string fileName, Window window)
//Convert Selected file
try
{
Dispose();
File = new DecodedFile();
if (!File.DecodeGm1File(Utility.FileToByteArray(userConfig.CrusaderPath + "\\" + fileName), fileName))
{
Expand Down

0 comments on commit b0744f9

Please sign in to comment.