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

Image.NewFromStream throws an exception #58

Closed
Gigas002 opened this issue Feb 2, 2020 · 4 comments
Closed

Image.NewFromStream throws an exception #58

Gigas002 opened this issue Feb 2, 2020 · 4 comments
Labels
bug Something isn't working
Milestone

Comments

@Gigas002
Copy link

Gigas002 commented Feb 2, 2020

I'm trying to read image from FileStream. Here's the example code:

Example app
using System;
using System.Threading.Tasks;
using NetVips;
using System.IO;

namespace Test
{
  public static class Program
  {
      static readonly Random Rnd = new Random();

      static Image RandomCrop(this Image image, int tileSize)
      {
          var x = Rnd.Next(0, image.Width);
          var y = Rnd.Next(0, image.Height);

          var width = Math.Min(tileSize, image.Width - x);
          var height = Math.Min(tileSize, image.Height - y);

          return image.Crop(x, y, width, height);
      }

      static void Main()
      {
          const string access = "sequential";
          const int tileSize = 256;
          const int threads = 6;

          // dimensions: 6569 x 3073
          var filePath = "4326.tif";
          var fileStream = File.OpenRead(filePath);
          // var huge = Image.NewFromFile(filePath, access: access);
          var huge = Image.NewFromStream(fileStream, access: access);

          var outputDirectoryPath = "output";
          Directory.CreateDirectory(outputDirectoryPath);

          Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = threads },
              i =>
              {
                  huge.RandomCrop(tileSize).WriteToFile($"{outputDirectoryPath}/x_{i}.png");

                  // var wfs = File.OpenWrite($"{outputDirectoryPath}/x_{i}.png");
                  // huge.RandomCrop(tileSize).WriteToStream(wfs, ".png");
              });


          Console.WriteLine("Done!");
      }
  }
}

Example input file.

This code throws an exception, while writing tiles. Exception text:

Process terminated. A callback was made on a garbage collected delegate of type 'NetVips!NetVips.Internal.VipsSourceCustom+SeekSignal::Invoke'.
Exception code=0xc0000005 flags=0x0 at 0x00007FFA3A789640. Access violation - attempting to read data at address 0x00000000000003D8

This happens only with NetVips 1.2.0, version 1.2.0-rc2 doesn't throw an exception and works fine.
I also tried running test with reading as Image.NewFromFile and writing as Image.WriteToStream (with writing to FileStream) and it works good, so it seems for me, that the error is only happens on reading.
Tested it on Windows 10 x64 and Ubuntu 18.04 x64.

P.S.
The exception is thrown not exactly on this line, it's pretty hard to catch. But I think it’s the line, that causes it.

@kleisauke kleisauke added the bug Something isn't working label Feb 4, 2020
@kleisauke kleisauke added this to the 1.2.1 milestone Feb 4, 2020
kleisauke added a commit that referenced this issue Feb 4, 2020
Free it when the image is unreferenced instead. This bug was introduced in v1.2.0.
@kleisauke
Copy link
Owner

Thanks for reporting this! This bug was introduced in 7a7543f. Wrapping the SourceStream within the using statement causes the ReadSignal / SeekSignal being re-located or disposed of by the garbage collector.

Commit 9f099f7 should fix this. If you want to test this, you can use the nightly version of NetVips. Add the https://ci.appveyor.com/nuget/net-vips feed in the <packageSources> section of your NuGet.config:

<packageSources>
  <add key="netvips-nightly" value="https://ci.appveyor.com/nuget/net-vips" />
</packageSources>

And update NetVips to 1.2.1 (build number 177 - prerelease). I'll make a new release soon.

@Gigas002
Copy link
Author

I've tested it with NetVips 1.2.1.177-develop + NetVips.Native 8.9.1 and it works perfecty. 👍

@musukvl
Copy link

musukvl commented Mar 1, 2020

1.2.0 Can't be used in production because of this bug. My application randomly goes down on load.

@kleisauke
Copy link
Owner

NetVips v1.2.1 is now available. Sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants