diff --git a/ReFuel.StbImage.csproj b/ReFuel.StbImage.csproj
index 628649d..7026d6a 100644
--- a/ReFuel.StbImage.csproj
+++ b/ReFuel.StbImage.csproj
@@ -14,7 +14,7 @@
True
ReFuel.StbImage
- 2.0.2-rc.0
+ 2.0.2-rc.1
STBI Authors, H. Utku Maden
A C# wrapper for the ubiquitous stb_image.h and stb_image_write.h library.
@@ -27,7 +27,9 @@
git
stb; stb_image; stbi; image; load; save; read; write
# 2.0.2
-* Fixed calling convention related execution engine exception for the Windows platform.
+* Fixed calling convention of unmanaged function pointers. (Thanks NogginBops!)
+* Allocating a GC handle to StbiStreamWrapper class and passing it as userdata into stbi in order to prevent
+ the object from being prematurely collected by the garbage collector when optimizations are enabled in Release mode.
# 2.0.1
* Enabled optimizations across the board for native and managed assemblies.
diff --git a/StbImage.cs b/StbImage.cs
index 46982cc..fc57153 100644
--- a/StbImage.cs
+++ b/StbImage.cs
@@ -209,17 +209,19 @@ namespace ReFuel.Stb
{
int x, y, iFormat;
StbiStreamWrapper wrapper = new StbiStreamWrapper(stream, true);
+ GCHandle gch = GCHandle.Alloc(wrapper, GCHandleType.Normal);
wrapper.CreateCallbacks(out stbi_io_callbacks cb);
stream.Position = 0;
IntPtr imagePtr;
if (asFloat)
{
- imagePtr = (IntPtr)Stbi.loadf_from_callbacks(&cb, null, &x, &y, &iFormat, (int)format);
+ imagePtr = (IntPtr)Stbi.loadf_from_callbacks(&cb, (void*)(IntPtr)gch, &x, &y, &iFormat, (int)format);
}
else
{
- imagePtr = (IntPtr)Stbi.load_from_callbacks(&cb, null, &x, &y, &iFormat, (int)format);
+ imagePtr = (IntPtr)Stbi.load_from_callbacks(&cb, (void*)(IntPtr)gch, &x, &y, &iFormat, (int)format);
}
+ gch.Free();
if (imagePtr != IntPtr.Zero)
{