使用Debug模式,按F5运行 程序,注意观察Output窗口,如果程序退出后,有内存泄露(Leak),则需delete,否则不需。

解决方案 »

  1.   

    必須在不再使用pEdit時 delete pEdit; 否則會造成內存泄露。
      

  2.   

    现在是不用call delete动作,并且也不产生内存泄露,我想知道在堆上分配的内存怎样被释放了!还是说new动作叫了CMyEdit类的operator new,从而不需要叫 delete的动作!
      

  3.   

    我觉得你的代码有问题。凡是在对种分配的内存,不用是一定要delete。
    同意huntout兄的看法。
      

  4.   

    事实上这样写确实没有内存漏洞的!(起码在我的debug 中没有提示)你想如果我想在视上创建一个就地编辑的编辑控件,那么你说我能在什么地方呼叫delete 的动作呢?你也可以试一下这样写,看看会不会有这样的问题,起码我这里一切都是那样的!谢谢
      

  5.   

    一般情况下是窗口自己删除自己。不知你编写过无模式对话框程序没有,有一种实现方法就是在WM_DESTROY或OnOK、OnCancel中使用delete this。所以在调用的时候只需要写new ,而不要delete。我估计这应该差不多。我想在CWnd类中,有一个标记是标明该窗口对象是否通过new来创建,如果是,则对删除(亦或破坏)窗口消息时,类本身的代码会调用delete this,将系统分配给自己的内存释放。
      

  6.   

    CEdit create 时必须指定父窗口,
    为何这样,就是为了管理,当父窗口
    关闭时,会自动释放下属窗口的资源
      

  7.   

    回复hyzx2000:有一点我还是不明白,为什么在类中添加一CEdit的对象的指针,然后在类的构造函数中new一个CEdit,一定还要在析构函数中delete 之;至于我用到的Edit类,我并未重载OnDestroy()之类的!所以即使被父窗口释放了,我想在别的case中父窗口也应该释放的吧!
      

  8.   

    我找了帮助,仔细看看。用new创建的对象,是分配在heap中的,必须delete;用create()创建的
    是分配在stack中的,绘自动销毁。
    If you create a CEdit object within a dialog box, the CEdit object is automatically destroyed when the user closes the dialog box.If you create a CEdit object from a dialog resource using the dialog editor, the CEdit object is automatically destroyed when the user closes the dialog box.If you create a CEdit object within a window, you may also need to destroy it. If you create the CEdit object on the stack, it is destroyed automatically. If you create the CEdit object on the heap by using the new function, you must call delete on the object to destroy it when the user terminates the Windows edit control. If you allocate any memory in the CEdit object, override the CEdit destructor to dispose of the allocations.
      

  9.   

    该到结束的时候了!终于我发现了自己的不小心,原来在"自己"的类中,重载过PostNcDestroy()此函数,相信各位已经知道我在这里做什么了吧!没错我用了delete this;这句!:D以至于在此窗口被消灭时,此C++对象也跟着delete了!不过这一招也太黑了一点了,搞的我狂在视中找delete动作!也害的大家劳累了!嘻嘻.不过我想我有了一点心得,起码是当你不能确定你new后创建的"窗口"何时会被销毁,以至于你无法做delete 动作时,那么就到PostNcDestroy()中做delete this;吧!(这一点也很感谢hyzx2000)令好象这个问题原来我也研究过,似乎还弄出来了,可是....
    问题解决了,30分就平分一下吧!下次多给点了,不多了!呵呵