1

我的表格上有一些按钮。当我单击每个按钮时,都会使用相同的按钮运行新表单。当我多次单击时,显示错误 OutOfMemory。
我认为这是因为我创建了很多表单对象。
如果有表格,可以清除堆栈或使用堆栈中的表格吗?

4

2 回答 2

3

您保留了指向旧组件的指针(引用),这会导致内存泄漏。确保永远不要将组件存储为类的成员,除非您稍后清除它们。

于 2011-11-27T10:35:13.367 回答
1

您需要为您的代码使用单例模式。在单例模式中,它只会创建表单类的一个对象。如果对象为空,那么它将创建一个新的 else ,它将返回当前的。为此,请参阅以下代码。

// Private Constructor

private static myForm thisForm = null;

private myForm()
{
     thisForm = this;
}

// Now to Create Object, you need to create following getInstance Method

public static myForm getInstance()
{
         if ( thisForm == null ) 
         {
                thisForm = new myForm();
         }
         return thisForm;
}

在整个代码中尝试上述逻辑。您的 OutOfMemory 问题将 100% 得到解决。

于 2011-11-25T02:13:19.947 回答