Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #58 from mahee96/master
Browse files Browse the repository at this point in the history
x64 fix [ClikeStringArray.cs] + improved Exception Handling
  • Loading branch information
kbilsted authored Sep 3, 2020
2 parents 9e85373 + 2886f5e commit f19af3a
Showing 1 changed file with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Kbg.NppPluginNET.PluginInfrastructure
{
Expand All @@ -18,10 +20,10 @@ public ClikeStringArray(int num, int stringCapacity)
for (int i = 0; i < num; i++)
{
IntPtr item = Marshal.AllocHGlobal(stringCapacity);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
_nativeItems.Add(item);
}
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (num * IntPtr.Size)), IntPtr.Zero);
Marshal.WriteIntPtr(_nativeArray + (num * IntPtr.Size), IntPtr.Zero);
}
public ClikeStringArray(List<string> lstStrings)
{
Expand All @@ -30,10 +32,10 @@ public ClikeStringArray(List<string> lstStrings)
for (int i = 0; i < lstStrings.Count; i++)
{
IntPtr item = Marshal.StringToHGlobalUni(lstStrings[i]);
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (i * IntPtr.Size)), item);
Marshal.WriteIntPtr(_nativeArray + (i * IntPtr.Size), item);
_nativeItems.Add(item);
}
Marshal.WriteIntPtr((IntPtr)((int)_nativeArray + (lstStrings.Count * IntPtr.Size)), IntPtr.Zero);
Marshal.WriteIntPtr(_nativeArray + (lstStrings.Count * IntPtr.Size), IntPtr.Zero);
}

public IntPtr NativePointer { get { return _nativeArray; } }
Expand All @@ -52,12 +54,19 @@ List<string> _getManagedItems(bool unicode)

public void Dispose()
{
if (!_disposed)
try
{
for (int i = 0; i < _nativeItems.Count; i++)
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
_disposed = true;
if (!_disposed)
{
for (int i = 0; i < _nativeItems.Count; i++)
if (_nativeItems[i] != IntPtr.Zero) Marshal.FreeHGlobal(_nativeItems[i]);
if (_nativeArray != IntPtr.Zero) Marshal.FreeHGlobal(_nativeArray);
_disposed = true;
}
}
catch (Exception e)
{
MessageBox.Show(MethodBase.GetCurrentMethod().ToString() +": "+ e.Message, this.GetType().Name);
}
}
~ClikeStringArray()
Expand Down

0 comments on commit f19af3a

Please sign in to comment.