Skip to content

Commit f850c60

Browse files
committed
Use managed objects instead of unsafe
1 parent 3b7d80b commit f850c60

2 files changed

Lines changed: 39 additions & 60 deletions

File tree

swig/include/csharp/csharp_strings.i

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ static CSharpUtf8StringHelperCallback SWIG_csharp_string_callback = NULL;
9797

9898
public static IntPtr DecodeStringToPinnedGCHandle(IntPtr pUtf8Bts) {
9999
string value = $module.StringEncoder?.FromNullTerminated(pUtf8Bts);
100-
if (value == null) return IntPtr.Zero;
100+
if (value == null)
101+
return IntPtr.Zero;
101102
var handle = System.Runtime.InteropServices.GCHandle.Alloc(value,
102103
System.Runtime.InteropServices.GCHandleType.Pinned);
103104
return System.Runtime.InteropServices.GCHandle.ToIntPtr(handle);
@@ -108,7 +109,8 @@ static CSharpUtf8StringHelperCallback SWIG_csharp_string_callback = NULL;
108109
public static readonly Utf8StringHelper utf8StringHelper = new Utf8StringHelper();
109110

110111
internal static string StringFromPinnedGCHandle(IntPtr pinnedHandle){
111-
if (pinnedHandle == IntPtr.Zero) return null;
112+
if (pinnedHandle == IntPtr.Zero)
113+
return null;
112114
var handle = System.Runtime.InteropServices.GCHandle.FromIntPtr(pinnedHandle);
113115
string value = handle.Target as string;
114116
handle.Free();
@@ -130,7 +132,7 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
130132
******************************************************************************/
131133

132134
%typemap(cstype) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) "string"
133-
%typemap(imtype) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) "IntPtr"
135+
%typemap(imtype, out="IntPtr") (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) "byte[]"
134136

135137
%typemap(in) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{
136138
$1 = ($1_ltype)$input;
@@ -144,15 +146,9 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
144146
$result = SWIG_csharp_string_callback((const char *)$1);
145147
%}
146148

147-
%typemap(csin,
148-
pre="
149-
byte[] bts$csinput = $module.StringEncoder?.ToNullTerminated($csinput);
150-
unsafe {
151-
fixed (byte* pBts$csinput = bts$csinput) {",
152-
terminator=" }}",
153-
cshin="$csinput"
154-
) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string)
155-
"(IntPtr)pBts$csinput"
149+
%typemap(csin) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{
150+
$module.StringEncoder?.ToNullTerminated($csinput)
151+
%}
156152

157153
%typemap(csout, excode=SWIGEXCODE) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string)
158154
{
@@ -167,18 +163,6 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
167163
* Typemap for UTF-8 char* string properties.
168164
*/
169165

170-
%typemap(csvarin, excode=SWIGEXCODE2) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{
171-
/* %typemap(csvarin) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) */
172-
set {
173-
byte[] bts$csinput = $module.StringEncoder?.ToNullTerminated($csinput);
174-
unsafe {
175-
fixed (byte* pBts$csinput = bts$csinput) {
176-
$imcall;$excode
177-
}
178-
}
179-
}
180-
%}
181-
182166
%typemap(csvarout, excode=SWIGEXCODE2) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) %{
183167
get {
184168
/* %typemap(csvarout) (char *), (char *&), (char[ANY]), (char[]), (const char *utf8_string) */
@@ -249,18 +233,13 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
249233
* caller doesn't want to see changes.
250234
*/
251235

252-
%typemap(imtype) (char **ignorechange) "ref IntPtr"
236+
%typemap(imtype) (char **ignorechange) "ref byte[]"
253237
%typemap(cstype) (char **ignorechange) "ref string"
254238
%typemap(csin,
255-
pre="
256-
byte[] bts$csinput = $module.StringEncoder?.ToNullTerminated($csinput);
257-
unsafe {
258-
fixed (byte* pBts$csinput = bts$csinput) {
259-
IntPtr temp$csinput = (IntPtr)pBts$csinput;",
260-
terminator=" }}",
239+
pre=" byte[] bts$csinput = $module.StringEncoder?.ToNullTerminated($csinput);",
261240
cshin="$csinput"
262241
) (char** ignorechange)
263-
"ref temp$csinput"
242+
"ref bts$csinput"
264243

265244
%typemap(in, noblock="1") (char **ignorechange)
266245
{
@@ -307,6 +286,21 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
307286
}
308287
GC.SuppressFinalize(this);
309288
}
289+
public static string[] DecodeStringArray(IntPtr pList) {
290+
int count = 0;
291+
if (pList != IntPtr.Zero) checked {
292+
while (System.Runtime.InteropServices.Marshal.ReadIntPtr(pList, count*IntPtr.Size) != IntPtr.Zero)
293+
count++;
294+
}
295+
string[] ret = new string[count];
296+
if (count > 0) {
297+
for(int cx = 0; cx < count; cx++) {
298+
IntPtr objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(pList, cx * IntPtr.Size);
299+
ret[cx]= $module.StringEncoder?.FromNullTerminated(objPtr);
300+
}
301+
}
302+
return ret;
303+
}
310304
}
311305
%}
312306

@@ -325,27 +319,6 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
325319
) char **options, char **dict, char **dictAndCSLDestroy, char **CSL
326320
"temp$csinput._ar"
327321

328-
/*
329-
* C# code to marshal NULL terminated lists of NULL terminated UTF-8 strings.
330-
*/
331-
332-
%define CS_MARSHAL_STRING_LIST()
333-
IntPtr cPtr = $imcall;
334-
IntPtr objPtr;
335-
int count = 0;
336-
if (cPtr != IntPtr.Zero) {
337-
while (System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, count*IntPtr.Size) != IntPtr.Zero)
338-
++count;
339-
}
340-
string[] ret = new string[count];
341-
if (count > 0) {
342-
for(int cx = 0; cx < count; cx++) {
343-
objPtr = System.Runtime.InteropServices.Marshal.ReadIntPtr(cPtr, cx * System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)));
344-
ret[cx]= $module.StringEncoder?.FromNullTerminated(objPtr);
345-
}
346-
}
347-
%enddef
348-
349322
/*
350323
* Marshal char**options, char **dict to string[] and don't free the unmanaged
351324
* string list.
@@ -354,7 +327,8 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
354327
%typemap(csout, excode=SWIGEXCODE) char**options, char **dict
355328
{
356329
/* %typemap(csout) char**options, char **dict */
357-
CS_MARSHAL_STRING_LIST()
330+
IntPtr cPtr = $imcall;
331+
string[] ret = $modulePINVOKE.StringListMarshal.DecodeStringArray(cPtr);
358332
$excode
359333
return ret;
360334
}
@@ -366,11 +340,16 @@ SWIGEXPORT void SWIGSTDCALL RegisterUtf8StringCallback_$module(CSharpUtf8StringH
366340

367341
%typemap(csout, excode=SWIGEXCODE) char** CSL, char **dictAndCSLDestroy {
368342
/* %typemap(csout) char** CSL, char **dictAndCSLDestroy */
369-
CS_MARSHAL_STRING_LIST()
370-
if (cPtr != IntPtr.Zero)
371-
$modulePINVOKE.StringListDestroy(cPtr);
372-
$excode
373-
return ret;
343+
IntPtr cPtr = $imcall;
344+
try {
345+
string[] ret = $modulePINVOKE.StringListMarshal.DecodeStringArray(cPtr);
346+
$excode
347+
return ret;
348+
}
349+
finally {
350+
if (cPtr != IntPtr.Zero)
351+
$modulePINVOKE.StringListDestroy(cPtr);
352+
}
374353
}
375354

376355
/*

swig/include/csharp/ogr_csharp.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DEFINE_EXTERNAL_CLASS(GDALMajorObjectShadow, OSGeo.GDAL.MajorObject)
7171
return new $csclassname(wkbGeometryType.wkbUnknown, null, 0, IntPtr.Zero, gml);
7272
}
7373

74-
public Geometry(wkbGeometryType type) : this(OgrPINVOKE.new_Geometry((int)type, IntPtr.Zero, 0, IntPtr.Zero, IntPtr.Zero), true, null) {
74+
public Geometry(wkbGeometryType type) : this(OgrPINVOKE.new_Geometry((int)type, default, 0, IntPtr.Zero, default), true, null) {
7575
if (OgrPINVOKE.SWIGPendingException.Pending) throw OgrPINVOKE.SWIGPendingException.Retrieve();
7676
}
7777
}

0 commit comments

Comments
 (0)