Add GCHandle to StbiStreamWrapper in TryLoad routines.
All checks were successful
Build / build (push) Successful in 1m56s

This commit is contained in:
H. Utku Maden 2024-11-18 20:17:31 +03:00
parent 09b5238b24
commit 6ffcd38cbc
2 changed files with 8 additions and 4 deletions

@ -14,7 +14,7 @@
<!-- Nuget Properties. -->
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>ReFuel.StbImage</PackageId>
<Version>2.0.2-rc.0</Version>
<Version>2.0.2-rc.1</Version>
<Authors>STBI Authors, H. Utku Maden</Authors>
<Description>
A C# wrapper for the ubiquitous stb_image.h and stb_image_write.h library.
@ -27,7 +27,9 @@
<RepositoryType>git</RepositoryType>
<PackageTags>stb; stb_image; stbi; image; load; save; read; write</PackageTags>
<PackageReleaseNotes># 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.

@ -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)
{