Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a single shared libvips binary on Windows #211

Closed
BiologyTools opened this issue Jul 6, 2023 · 11 comments
Closed

Support a single shared libvips binary on Windows #211

BiologyTools opened this issue Jul 6, 2023 · 11 comments
Labels
enhancement New feature or request
Milestone

Comments

@BiologyTools
Copy link

I'm trying to use libvips using net-vips I have gotten libvips to work on Mac & Linux but on windows when trying to open a tiff file I get a System.AccessViolationException. I should have the proper environmental variables $VIPS_HOME, etc. set. Here is the error: System.AccessViolationException
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any suggestions on how to get libvips to work on windows?

@kleisauke kleisauke added the triage This issue is being investigated label Jul 7, 2023
@kleisauke
Copy link
Owner

Does this happen for all TIFF images? Are you able to provide a complete, standalone code sample that allows someone else to reproduce this issue?

Note that there is no requirement to set the VIPSHOME or PATH environment variables when using the prebuilt binaries provided by the NetVips.Native NuGet package.

@BiologyTools
Copy link
Author

BiologyTools commented Jul 8, 2023

I think the problem has something to do with the current environment set up. Since without $VIPS_HOME etc. setup I couldn't get lib_vips to start at all giving the error DllNotFoundException on windows. I haven't tested all different types of tiff files, but with the pyramidal tiff's I have they all throw the System.AccessViolationException on windows. Here is the code that causes the error

public static bool VipsSupport(BioImage bi)
        {
            NetVips.Image im;
            try
            {
                im = NetVips.Image.Tiffload(bi.file);
                Settings.AddSettings("VipsSupport", "true");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }
            im.Dispose();
            return true;
        }

I have the nuget packages installed including NetVips.Native and NetVips.Native.win-x64. My project uses .NET6.

@BiologyTools
Copy link
Author

BiologyTools commented Jul 8, 2023

Looks like this something to do with GTKSharp or something specific to my application as I can run netvips on windows before the application launches GTKSharp. If I use NetVips before initializing GTK libvips does something which causes GTKSharp to throw various errors. Like: "Procedure start location g_regex_match_simple could not be found in dynamicly linked library C:msys64\mingw64\bin\libgtk-3.0.dll"

@BiologyTools
Copy link
Author

BiologyTools commented Jul 8, 2023

I should probably open this issue in GTKSharp? Or do you know how I can fix this environmental error? Any suggestions are welcome?

//Works here
bool vips = BioImage.VipsSupport("F:\\Young_mouse\\MouseSmall.tif");
Console.WriteLine("Initializing GTK.");
//If I use netvips before Init it works but then GTK Init will fail.
Application.Init();

@kleisauke
Copy link
Owner

It looks like this is a DLL conflict between the prebuilt GLib binaires provided by NetVips (i.e. libglib-2.0-0.dll and libgobject-2.0-0.dll) and the ones provides by GtkSharp (located in %localappdata%\Gtk\3.24.24).

Building a single shared libvips binary on Windows would probably fix this (see e.g. commit libvips/build-win64-mxe@208bd40). However, this would break compatibility with other libvips bindings and the existing shared prebuilt binaries.

// We cannot define all these variables as `libvips-42.dll` without
// breaking compatibility with the shared Windows build. Therefore,
// we always ship at least 3 DLLs.
internal const string GLib = "libglib-2.0-0.dll",
GObject = "libgobject-2.0-0.dll",
Vips = "libvips-42.dll";

Linux and macOS doesn't have this issue after commit e04c6b0.

@BiologyTools
Copy link
Author

OK I guess I will suggest that my users download the windows only version instead of GTK to use vips on windows.

@kleisauke kleisauke added wontfix This will not be worked on and removed triage This issue is being investigated labels Sep 27, 2023
@kleisauke
Copy link
Owner

I think that's a reasonable workaround.

However, this would break compatibility with other libvips bindings and the existing shared prebuilt binaries.

With .NET version 6.0 or higher, one can implement fallback logic using NativeLibrary.SetDllImportResolver.

// For Windows, we try to locate the GLib symbols within
// `libvips-42.dll` first. If these symbols cannot be found there,
// we proceed to locate them within `libglib-2.0-0.dll` and
// `libgobject-2.0-0.dll`. Note that distributing a single shared
// library is only possible when we drop support for .NET Framework.
// Therefore, we always ship at least 3 DLLs for now.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return _gLibStaticallyLinked ? Libraries.Vips : libraryName;
}

But this won't work on .NET Framework. Additionally, it might be a bit too early to stop supporting .NET Standard 2.0 and Mono (see commit 3a5d427).

I'll close this as "won't fix" for now, sorry.

@kleisauke kleisauke closed this as not planned Won't fix, can't repro, duplicate, stale Sep 27, 2023
@kleisauke kleisauke changed the title LibVips Windows TiffLoad System.AccessViolationException Support a single shared libvips binary on Windows Oct 14, 2024
@kleisauke kleisauke added enhancement New feature or request and removed wontfix This will not be worked on labels Oct 14, 2024
kleisauke added a commit that referenced this issue Oct 14, 2024
@kleisauke
Copy link
Owner

Re-opening now that commit 2f4f658 and 1448040 was landed. This will be available in NetVips 3.0.0 (RSN).

@kleisauke kleisauke reopened this Oct 14, 2024
@kleisauke kleisauke added this to the 3.0.0 milestone Oct 14, 2024
@kleisauke
Copy link
Owner

kleisauke commented Oct 14, 2024

A release candidate of NetVips 3.0.0 and NetVips.Native 8.16.0 is now available for testing.
https://www.nuget.org/packages/NetVips/3.0.0-rc1
https://www.nuget.org/packages/NetVips.Native/8.16.0-rc1

@kleisauke
Copy link
Owner

NetVips v3.0.0 and NetVips.Native v8.16.0 is now available.

@BiologyTools
Copy link
Author

Thank you so much for making this change now I can use GTK# and netvips on windows. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants