`

SWT中org.eclipse.swt.SWTError: No more handles

 
阅读更多
在RCP开发中如果牵涉到 Color,Image,Font,Cursor 等的过频的操作或者数据量比较大时  出现该异常的可能性就很大了。

由于这些类的对象 每new一个就要消耗掉一个handler ,如果没有及时的dispose,而系统分配的handler数量有限,于是出现了该错误。

============

解决办法就是用缓存,在java里面可以用Map集合的形式来处理,把用到了每一个对象分别放到对应的Map中,在不需要时,对其dispose同时clear Map集合。

==================

缓存文件结构大概如下:

public class CacheManager {
 //  colorMap
 public static Map<RGB, Color> colorMap = new HashMap<RGB, Color>();

 public static Color getColor(int systemColorID) {
  Display display = Display.getCurrent();
  return display.getSystemColor(systemColorID);
 }

 
 public static Color getColor(int r, int g, int b) {
  return getColor(new RGB(r, g, b));
 }

 public static Color getColor(RGB rgb) {
  Color color = colorMap.get(rgb);
  if (color == null) {
   Display display = Display.getCurrent();
   color = new Color(display, rgb);
   colorMap.put(rgb, color);
  }
  return color;
 }

 public static void disposeColors() {
  for (Color color : colorMap.values()) {
   color.dispose();
  }
  colorMap.clear();
 }

//-----------------------------------------
// imageMap

public static Map<String, Image> imageMap = new HashMap<String, Image>();

 protected static Image getImage(InputStream stream) throws IOException {
  try {
   Display display = Display.getCurrent();
   ImageData data = new ImageData(stream);
   if (data.transparentPixel > 0) {
    return new Image(display, data, data.getTransparencyMask());
   }
   return new Image(display, data);
  } finally {
   stream.close();
  }
 }

 public static Image getImage(String relativePathName) {//这个参数是对插件项目而言的,比如可以是:"icons/xxx.jpg"等

  //将插件项目中的 文件相对路径转换成 绝对路径的转换过程

  Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
  URL url = bundle.getResource(relativePathName);
  String fullPathString = null;
  try {
   fullPathString = FileLocator.toFileURL(url).toURI().toString();
  } catch (Exception e1) {
   e1.printStackTrace();
  }
  fullPathString = fullPathString.replaceFirst("file:/", "");
  Image image = imageMap.get(fullPathString);
  if (image == null) {
   try {
    image = getImage(new FileInputStream(fullPathString.toString()));
    imageMap.put(fullPathString, image);
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return image;
 }


 public static void disposeImages() {
  // dispose loaded images

  for (Image image : imageMap.values()) {
    image.dispose();
   }
   imageMap.clear();
  }

//-----------------------------------------
//fontMap

//...

//-----------------------------------------

//cursorsMap

//....

//------------------------------------------

/**
  * Dispose All
  */

public static void dispose() {
  disposeColors();
  disposeImages();
  //...

  //...

 }

}
分享到:
评论

相关推荐

    GUI_handles.zip_GUI_handles_GUI_handles.zip _MATLAB GUI源代码_Matla

    "GUI_handles.zip"是一个包含MATLAB GUI源代码的压缩包,特别针对"handles"这一主题,旨在帮助用户深入理解并熟练掌握MATLAB GUI的编写。 在MATLAB GUI中,"handles"是一个关键概念,它是一种数据结构,存储了GUI...

    flex-object-handles.zip_flex

    在Flex开发中,"flex-object-handles.zip_flex"这个压缩包可能包含了关于如何操作和交互Flex中的对象,特别是涉及到对象的移动、编译、放大和缩小功能的代码示例或教程。Flex是一种基于ActionScript和MXML的开源框架...

    关于Unhandled event loop exception No more handles的两种解决方案

    在使用Eclipse开发工具的过程中,有时会遇到一个较为棘刺的问题——出现“Unhandled event loop exception No more handles”的错误提示。这一错误不仅令人困惑,而且严重影响了Eclipse的正常使用。本文旨在通过分析...

    xulrunner-1.9.2.28pre.en-US.linux-x86_64.tar.rar

    org.eclipse.swt.SWTError: No more handles [MOZILLA_FIVE_HOME=''] (java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: no swt-mozilla-gtk-4335 in java.library.path no swt-mozilla-gtk ...

    xulrunner-1.9.2.28pre.en-US.linux-x86_64.tar

    解决org.eclipse.swt.SWTError: No more handles [MOZILLA_FIVE_HOME=''] (java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons: no swt-mozilla-gtk-4335 in java.library.path

    java的常见问题及解决方法参照.pdf

    如果在Java应用中使用XULRunner嵌入浏览器,但遇到"Exception in thread "main" org.eclipse.swt.SWTError: No more handles [Could not detect registered XULRunner to use]",你需要确保XULRunner已经正确注册。...

    java的常见问题及解决方法定义.pdf

    在Java应用中嵌入浏览器时,如果遇到“Exception in thread "main" org.eclipse.swt.SWTError: No more handles [Could not detect registered XULRunner to use]”的异常,意味着XULRunner未注册。在Windows环境下...

    gef 转折线的相关方法实现和 GEF的API chm 格式

    - `org.eclipse.gef.handles`包下的类:用于创建图形元素的手柄,用户可以通过手柄进行交互操作,如拖动转折点。 GEF的API文档通常以CHM(Compiled HTML Help)格式提供,这是一种Windows平台下的帮助文档格式,...

Global site tag (gtag.js) - Google Analytics