Rococoa 位置的内存访问无效

发布于 2024-08-09 08:14:17 字数 2818 浏览 7 评论 0原文

我一直在尝试使用 rococoa (java 到 osx cocoa api 库)编写一个简单的屏幕截图应用程序,并设法实际截取屏幕截图,然后将其保存到文件中。不幸的是,应用程序偶尔会失败,并出现“位置内存访问无效...”错误。我假设这是由于垃圾收集造成的,因为我无法保持引用处于活动状态。导致崩溃的行是: int[] data =pointer.getIntArray(0, bytesPerPlane / 4);

我真的没有用 Objective C 编写过任何代码,只是从 rococoa 开始,所以我发现自己对此感到困惑。我复制了下面的相关代码,非常感谢任何帮助!


public interface QuartzLibrary extends Library {

    QuartzLibrary INSTANCE = (QuartzLibrary) Native.loadLibrary("Quartz", QuartzLibrary.class);

    class CGPoint extends Structure {
        public double x;
        public double y;
    }

    class CGSize extends Structure {
        public double width;
        public double height;
    }

    class CGRect extends Structure implements Structure.ByValue {
        public static class CGRectByValue extends CGRect { }

        public CGPoint origin;
        public CGSize size;
    }

    int kCGWindowListOptionIncludingWindow = (1 << 3);
    int kCGWindowImageBoundsIgnoreFraming = (1 << 0);

    ID CGWindowListCreateImage(CGRect screenBounds, int windowOption, int windowId, int imageOption);
}

public interface NSBitmapImageRep extends NSObject {

    public static final _Class CLASS = Rococoa.createClass("NSBitmapImageRep", _Class.class);

    public interface _Class extends NSClass {
        NSBitmapImageRep alloc();
    }

    NSBitmapImageRep initWithCGImage(ID imageRef);
    com.sun.jna.Pointer bitmapData();
    NSSize size();
}

public class Screenshot {

    public static void getScreenshot(int windowId) throws IOException {
        QuartzLibrary.CGRect bounds = new QuartzLibrary.CGRect.CGRectByValue();
        bounds.origin = new QuartzLibrary.CGPoint();
        bounds.origin.x = 0;
        bounds.origin.y = 0;
        bounds.size = new QuartzLibrary.CGSize();
        bounds.size.width = 0;
        bounds.size.height = 0;
        ID imageRef = QuartzLibrary.INSTANCE.CGWindowListCreateImage(bounds, QuartzLibrary.kCGWindowListOptionIncludingWindow, windowId, QuartzLibrary.kCGWindowImageBoundsIgnoreFraming);

        NSBitmapImageRep imageRep = NSBitmapImageRep.CLASS.alloc();
        imageRep = imageRep.initWithCGImage(imageRef);
        NSSize size = imageRep.size();
        com.sun.jna.Pointer pointer = imageRep.bitmapData();

        int width = size.width.intValue();
        int height = size.height.intValue();

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // The crash always happens when calling 'getIntArray' in the next line.
        int[] data = pointer.getIntArray(0, bytesPerPlane / 4);
        int idx = 0;
        for(int y = 0; y < height; y++)
            for(int x = 0; x < width; x++)
                image.setRGB(x, y, data[idx++]);

        ImageIO.write(image, "png", new File("foo.png"));
    }
}

I've been trying to code a simple screenshot application using rococoa (java to osx cocoa api library), and managed to get as far as actually taking the screenshot, and then saving it to a file. Unfortunately, once in a while, the application fails with an 'Invalid memory access of location...' error. I'm assuming this is due to something being garbage collected, because I'm failing to keep a reference alive. The line that is causing the crash is:
int[] data = pointer.getIntArray(0, bytesPerPlane / 4);

I really haven't coded anything with Objective C, and just starting up with rococoa, so I'm finding myself just confused with this. I've copied the relevant code below, and would really appreciate any help with this!


public interface QuartzLibrary extends Library {

    QuartzLibrary INSTANCE = (QuartzLibrary) Native.loadLibrary("Quartz", QuartzLibrary.class);

    class CGPoint extends Structure {
        public double x;
        public double y;
    }

    class CGSize extends Structure {
        public double width;
        public double height;
    }

    class CGRect extends Structure implements Structure.ByValue {
        public static class CGRectByValue extends CGRect { }

        public CGPoint origin;
        public CGSize size;
    }

    int kCGWindowListOptionIncludingWindow = (1 << 3);
    int kCGWindowImageBoundsIgnoreFraming = (1 << 0);

    ID CGWindowListCreateImage(CGRect screenBounds, int windowOption, int windowId, int imageOption);
}

public interface NSBitmapImageRep extends NSObject {

    public static final _Class CLASS = Rococoa.createClass("NSBitmapImageRep", _Class.class);

    public interface _Class extends NSClass {
        NSBitmapImageRep alloc();
    }

    NSBitmapImageRep initWithCGImage(ID imageRef);
    com.sun.jna.Pointer bitmapData();
    NSSize size();
}

public class Screenshot {

    public static void getScreenshot(int windowId) throws IOException {
        QuartzLibrary.CGRect bounds = new QuartzLibrary.CGRect.CGRectByValue();
        bounds.origin = new QuartzLibrary.CGPoint();
        bounds.origin.x = 0;
        bounds.origin.y = 0;
        bounds.size = new QuartzLibrary.CGSize();
        bounds.size.width = 0;
        bounds.size.height = 0;
        ID imageRef = QuartzLibrary.INSTANCE.CGWindowListCreateImage(bounds, QuartzLibrary.kCGWindowListOptionIncludingWindow, windowId, QuartzLibrary.kCGWindowImageBoundsIgnoreFraming);

        NSBitmapImageRep imageRep = NSBitmapImageRep.CLASS.alloc();
        imageRep = imageRep.initWithCGImage(imageRef);
        NSSize size = imageRep.size();
        com.sun.jna.Pointer pointer = imageRep.bitmapData();

        int width = size.width.intValue();
        int height = size.height.intValue();

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // The crash always happens when calling 'getIntArray' in the next line.
        int[] data = pointer.getIntArray(0, bytesPerPlane / 4);
        int idx = 0;
        for(int y = 0; y < height; y++)
            for(int x = 0; x < width; x++)
                image.setRGB(x, y, data[idx++]);

        ImageIO.write(image, "png", new File("foo.png"));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澜川若宁 2024-08-16 08:14:17

发现问题了。

‘imageRep’的最后一次使用上线了
“com.sun.jna.Pointer 指针 = imageRep.bitmapData();”。
此后,imageRep 对于 Java 垃圾收集器来说是公平的游戏。如果它击中
在我们使用完“指针”之前,它指向的后备缓冲区可能/将会得到
被释放,导致不好的事情发生。
要修复此问题,请为 imageRep 添加一组额外的保留/释放即可完成此工作,或者
将对其的任何引用添加到方法的末尾。

Found the problem.

The last use of 'imageRep' is on the line
"com.sun.jna.Pointer pointer = imageRep.bitmapData();".
After this, imageRep is fair game for the Java garbage collector. And if it hits in
before we are done using 'pointer', the backing buffer it's pointing to may/will get
freed, causing bad things to happen.
To fix it, adding an extra set of retain/release for imageRep does the job, or alternatively
adding any reference to it to the end of the method.

倾听心声的旋律 2024-08-16 08:14:17

可能相关,也可能不相关:NSBitmapImageRep 源自 NSImageRep,而不是直接源自 NSObject。

Possibly related, possibly not: NSBitmapImageRep descends from NSImageRep, not directly from NSObject.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文