在Mac OS X上无法获得SWT显示 [英] Can't get SWT Display on Mac OS X

查看:294
本文介绍了在Mac OS X上无法获得SWT显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Mac OS X Snow Leopard,并且无法从OSGi捆绑包中的激活器访问Display.

I'm running Mac OS X Snow Leopard and wan't to access the Display from the activator in an OSGi bundle.

下面是激活器的启动方法:

Below is the start method for my activator:

@Override
public void start(BundleContext context) throws Exception {
    ExecutorService service = Executors.newSingleThreadExecutor();
    service.execute(new Runnable() {

        @Override
        public void run() {
            Display display = Display.getDefault();
            Shell shell = new Shell(display);
            Text helloText = new Text(shell, SWT.CENTER);
            helloText.setText("Hello SWT!");
            helloText.pack();
            shell.pack();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    });
}

在Windows环境中调用此代码可以正常工作,但是在Mac OS X上进行部署时,我得到以下输出:

Calling this code in a Windows environment works fine, but deploying on Mac OS X I get the following output:


2009-10-14 17:17:54.050 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x101620d20 of class NSCFString autoreleased with no pool in place - just leaking
2009-10-14 17:17:54.081 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x100119240 of class NSCFNumber autoreleased with no pool in place - just leaking
2009-10-14 17:17:54.084 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x1001024b0 of class NSCFString autoreleased with no pool in place - just leaking
2009-10-14 17:17:54.086 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x7fff701d7f70 of class NSCFString autoreleased with no pool in place - just leaking
2009-10-14 17:17:54.087 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x100113330 of class NSCFString autoreleased with no pool in place - just leaking
2009-10-14 17:17:54.092 java[2010:10003] *** __NSAutoreleaseNoPool(): Object 0x101624540 of class NSCFData autoreleased with no pool in place - just leaking
.
.
.

我使用-XstartOnFirstThread VM参数没有任何运气.我使用的是64位可可,但我也尝试过使用32位的Cocoa.

I've used the -XstartOnFirstThread VM argument without any luck. I'm on 64-bit Cocoa but I've also tried 32-bit Cocoa.

尝试使用Carbon时,出现以下错误:

When trying on Carbon I get the following error:


Invalid memory access of location 00000020 eip=9012337c

在调试Display类时,我可以看到Displays []数组仅包含空引用.

When debugging into the Display class I can see that the Displays[] array only contains null references.

推荐答案

我可以确认我们在Mac OS X上成功完成了SWT Carbon ,这是由捆绑激活启动的,它本身具有事件循环功能,因此绝对有可能!启动VM时,它正在使用-XstartOnFirstThread.

I can confirm that we successfully run SWT Carbon on Mac OS X in its own event loop kicked off by a bundle activation, so it's definitely possible! This is using -XstartOnFirstThread when launching the VM.

但是,对于可可SWT(64位),我看到了相同的错误:(

But, with Cocoa SWT (64-bit), I see the same error :(

似乎,尽管我们运行Carbon SWT的方式有效,但可能不是合乎逻辑的:我们正在通过另一个线程(而不是您应该的主线程)驱动事件循环.在可可SWT下,这种方法不再起作用,而且无论如何可能都是狡猾的做法.

It seems that, although the way we ran Carbon SWT worked, it was probably not kosher: we were driving the event loop through another thread, not the main one as you're supposed to. Under Cocoa SWT, this doesn't work any more, and it was probably dodgy practice anyway.

我可以在创建Display(通过Cocoa SWT Device构造函数改编)之前用以下技巧解决线程池错误:

I can fix the thread pool errors with the following hack before creating the Display (adapted from the Cocoa SWT Device constructor):

  NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
  NSThread nsthread = NSThread.currentThread();
  NSMutableDictionary dictionary = nsthread.threadDictionary();
  NSString key = NSString.stringWith("SWT_NSAutoreleasePool");
  id obj = dictionary.objectForKey(key);
  if (obj == null) {
          NSNumber nsnumber = NSNumber.numberWithInteger(pool.id);
          dictionary.setObject(nsnumber, key);
  } else {
          pool.release();
  }

但是,随后的事件循环挂起(即display.readAndDispatch()/display.sleep()跳舞).我怀疑它不是主线程,所以只是不读取UI事件.

However, the event loop that follows hangs (i.e. the display.readAndDispatch ()/display.sleep () dance). I suspect it's just not reading UI events due not being the main thread.

我不确定是否有解决该问题的方法.就我而言,我们控制启动OSGi的主要JVM线程,因此我想在其中添加一个挂钩,以便在OSGi启动后运行SWT事件循环.

I'm not sure if there's a kosher way to fix this. In my case, we control the main JVM thread that launches OSGi, so I'm toying with the idea of adding a hook in there that can run the SWT event loop after OSGi launch.

这篇关于在Mac OS X上无法获得SWT显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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