Rococoa对位置的内存访问无效 [英] Invalid memory access of location with Rococoa

查看:226
本文介绍了Rococoa对位置的内存访问无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

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

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"));
    }
}


推荐答案

找到问题。

'imageRep'的最后一次使用是在行
com.sun.jna.Pointer pointer = imageRep.bitmapData(); 。
在此之后,imageRep是Java垃圾收集器的公平游戏。如果在我们使用'指针'之前它在
中命中,那么它指向的后备缓冲区可能/将释放
,从而导致坏事发生。
要修复它,为imageRep添加一组额外的保留/释放工作,或者
将任何引用添加到方法的末尾。

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.

这篇关于Rococoa对位置的内存访问无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆