Skip to content

Commit a37f2f6

Browse files
committed
Code: Sends copy of string to upstream.
LibSass changed this behavior recently that it will *own* the memory we are passing to it. this delta makes changes to `MarshalString()` function so it returns a copy of `char*` after freeing the converted `char*` from `String^`. Then it removes the unrequired `FreeString()` calls in `finally` block to avoid "double-freeing". Fixes #28
1 parent ff58049 commit a37f2f6

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

libsass/SassInterface.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ namespace LibSassNet
6767
finally
6868
{
6969
// Free resources
70-
FreeString(includePaths);
71-
FreeString(sourceString);
7270
sass_delete_data_context(ctx);
7371
}
7472
}
@@ -127,9 +125,6 @@ namespace LibSassNet
127125
finally
128126
{
129127
// Free resources
130-
FreeString(includePaths);
131-
FreeString(inputPath);
132-
FreeString(mapFile);
133128
sass_delete_file_context(ctx);
134129
}
135130
}
@@ -156,6 +151,7 @@ namespace LibSassNet
156151
}
157152
finally
158153
{
154+
// Upstream will not free the memory in case of sass2scss
159155
FreeString(sourceText);
160156
}
161157
}

libsass/StringToANSI.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//SOFTWARE.
2020

2121
#using <System.dll>
22+
#include <string>
2223
#include "StringToANSI.hpp"
2324

2425
using namespace System;
@@ -27,8 +28,18 @@ using namespace System::Runtime::InteropServices;
2728
namespace LibSassNet
2829
{
2930
char* MarshalString(String^ s)
30-
{
31-
return (char*) ((Marshal::StringToCoTaskMemAnsi(s)).ToPointer());
31+
{
32+
if (!s) {
33+
return nullptr;
34+
}
35+
36+
char* original_str = (char*)(Marshal::StringToCoTaskMemAnsi(s)).ToPointer();
37+
char* target_str = (char*)malloc(strlen(original_str) + 1);
38+
strcpy(target_str, original_str);
39+
40+
FreeString(original_str);
41+
42+
return target_str;
3243
}
3344

3445
void FreeString(const char* p)

0 commit comments

Comments
 (0)