11// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
22using System ;
33using System . Collections . Generic ;
4+ using System . Reflection ;
45using System . Runtime . InteropServices ;
6+ using System . Windows . Forms ;
57
68namespace Kbg . NppPluginNET . PluginInfrastructure
79{
@@ -18,10 +20,10 @@ public ClikeStringArray(int num, int stringCapacity)
1820 for ( int i = 0 ; i < num ; i ++ )
1921 {
2022 IntPtr item = Marshal . AllocHGlobal ( stringCapacity ) ;
21- Marshal . WriteIntPtr ( ( IntPtr ) ( ( int ) _nativeArray + ( i * IntPtr . Size ) ) , item ) ;
23+ Marshal . WriteIntPtr ( _nativeArray + ( i * IntPtr . Size ) , item ) ;
2224 _nativeItems . Add ( item ) ;
2325 }
24- Marshal . WriteIntPtr ( ( IntPtr ) ( ( int ) _nativeArray + ( num * IntPtr . Size ) ) , IntPtr . Zero ) ;
26+ Marshal . WriteIntPtr ( _nativeArray + ( num * IntPtr . Size ) , IntPtr . Zero ) ;
2527 }
2628 public ClikeStringArray ( List < string > lstStrings )
2729 {
@@ -30,10 +32,10 @@ public ClikeStringArray(List<string> lstStrings)
3032 for ( int i = 0 ; i < lstStrings . Count ; i ++ )
3133 {
3234 IntPtr item = Marshal . StringToHGlobalUni ( lstStrings [ i ] ) ;
33- Marshal . WriteIntPtr ( ( IntPtr ) ( ( int ) _nativeArray + ( i * IntPtr . Size ) ) , item ) ;
35+ Marshal . WriteIntPtr ( _nativeArray + ( i * IntPtr . Size ) , item ) ;
3436 _nativeItems . Add ( item ) ;
3537 }
36- Marshal . WriteIntPtr ( ( IntPtr ) ( ( int ) _nativeArray + ( lstStrings . Count * IntPtr . Size ) ) , IntPtr . Zero ) ;
38+ Marshal . WriteIntPtr ( _nativeArray + ( lstStrings . Count * IntPtr . Size ) , IntPtr . Zero ) ;
3739 }
3840
3941 public IntPtr NativePointer { get { return _nativeArray ; } }
@@ -52,12 +54,19 @@ List<string> _getManagedItems(bool unicode)
5254
5355 public void Dispose ( )
5456 {
55- if ( ! _disposed )
57+ try
5658 {
57- for ( int i = 0 ; i < _nativeItems . Count ; i ++ )
58- if ( _nativeItems [ i ] != IntPtr . Zero ) Marshal . FreeHGlobal ( _nativeItems [ i ] ) ;
59- if ( _nativeArray != IntPtr . Zero ) Marshal . FreeHGlobal ( _nativeArray ) ;
60- _disposed = true ;
59+ if ( ! _disposed )
60+ {
61+ for ( int i = 0 ; i < _nativeItems . Count ; i ++ )
62+ if ( _nativeItems [ i ] != IntPtr . Zero ) Marshal . FreeHGlobal ( _nativeItems [ i ] ) ;
63+ if ( _nativeArray != IntPtr . Zero ) Marshal . FreeHGlobal ( _nativeArray ) ;
64+ _disposed = true ;
65+ }
66+ }
67+ catch ( Exception e )
68+ {
69+ MessageBox . Show ( MethodBase . GetCurrentMethod ( ) . ToString ( ) + ": " + e . Message , this . GetType ( ) . Name ) ;
6170 }
6271 }
6372 ~ ClikeStringArray ( )
0 commit comments