关于destroy子窗口的内存释放问题 Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

关于destroy子窗口的内存释放问题

Post by swallow » Thu Jun 19, 2008 1:08 pm

请教wxWidgets的垃圾回收是怎么处理的,把父窗口的子窗口用destroy销毁以后,内存并不能马上释放,而且父窗口占的内存会增加,多次打开销毁子窗口,会导致父窗口占的内存越来越大,有没有办法解决这个问题?

Utensil
Moderator
Moderator
Posts: 423
Joined: Sun Feb 03, 2008 11:38 am
Location: China

Re: 关于destroy子窗口的内存释放问题

Post by Utensil » Sat Jun 21, 2008 1:42 am

把父窗口的子窗口用destroy销毁以后,内存的确不会马上释放,因为destroy只是将销毁这个窗口的动作放在了一个队列中,等待子窗口的所有事件处理完才会把它销毁。

不过会“多次打开销毁子窗口,导致父窗口占的内存越来越大”这个现象我没有体验过,我有一个程序需要频繁打开销毁子窗口的,并没有出现这个问题。

-Utensil
In fascination of creating worlds by words, and in pursuit of words behind the world.

On Github: http://utensil.github.com
Technical Blog in Chinese: http://utensil.iteye.com/

swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

能告诉我Destroy和Close和Hide有哪些差别么?

Post by swallow » Tue Jun 24, 2008 11:59 am

能告诉我Destroy和Close和Hide有哪些差别么?

Utensil
Moderator
Moderator
Posts: 423
Joined: Sun Feb 03, 2008 11:38 am
Location: China

Re: 能告诉我Destroy和Close和Hide有哪些差别么?

Post by Utensil » Tue Jun 24, 2008 12:15 pm

swallow wrote:能告诉我Destroy和Close和Hide有哪些差别么?
如果把这比作辨析题的话,Destroy只需要和delete相区别。

Close只是发给窗口一个关闭事件,窗口会对这个事件予以处理,你也自己可以处理。但你处理的话,少不得要在处理函数里面调用一次Destroy。

Hide就更远了,只是隐藏窗口,相当于Show(false)。

这些在文档中都给出了详细的辨析,请参见http://docs.wxwidgets.org/stable/wx_wxw ... indowclose
In fascination of creating worlds by words, and in pursuit of words behind the world.

On Github: http://utensil.github.com
Technical Blog in Chinese: http://utensil.iteye.com/

swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

Post by swallow » Wed Jul 02, 2008 12:50 pm

我到现在也没找到解决内存问题的办法,内存泄漏非常严重,总死机,能告诉我你用的是哪个版本的wxWidgets么?我用的是2.6.3,我打开主窗口,然后点击主窗口上的一个按钮打开子窗口,假如消耗内存100k,用destroy销毁子窗口后内存只减少了20k,不知那80k哪里去了。窗口里只有位图按钮的时候还能减少这20k,如果用的是普通按钮,销毁子窗口后内存反而要增加20k,不知怎么回事。能给我一个打开子窗口并销毁子窗口的例子么?

kevin_fly
Experienced Solver
Experienced Solver
Posts: 50
Joined: Tue Apr 15, 2008 12:55 am
Contact:

Post by kevin_fly » Thu Jul 03, 2008 1:13 am

我的界面做得也算复杂的了,但使用正常(也有内存泄漏),没有死机或者速度慢等现象,我使用的是2.8.7版本,我机器配置是Inter双核处理器,1G内存。
http://blog.163.com/kevin_fly/
-----我的博客,我的知识库------

Utensil
Moderator
Moderator
Posts: 423
Joined: Sun Feb 03, 2008 11:38 am
Location: China

Post by Utensil » Thu Jul 03, 2008 4:39 am

不是很能理解为什么楼主会遭遇这样的现象...C++并不依赖垃圾回收,所有的回收都是程序员自己做,除非是代码本身的问题,否则不会出现这种问题的。而就目前使用wxWidgets2.8.7的经历来看,没有发现wxWidgets本身出现这个问题。

如果楼主的源代码行数很多,不妨试着以最少的代码里重新制造出这个现象,然后我们帮楼主分析分析....

-Utensil
In fascination of creating worlds by words, and in pursuit of words behind the world.

On Github: http://utensil.github.com
Technical Blog in Chinese: http://utensil.iteye.com/

swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

Post by swallow » Thu Jul 03, 2008 1:39 pm

我用的WCE内存只有30M,所以总死机,在PC机下倒是无所谓,我现在手边没代码,明天把代码贴上来,大家帮我分析分析。

swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

Post by swallow » Sun Jul 06, 2008 6:11 am

我用的VS2005写程序。
打开主窗口

Code: Select all

dialog5* mainWindow = new dialog5(NULL, ID_DIALOG5, _("dialog5"));
mainWindow->Show();
打开子窗口

Code: Select all

void dialog5::OnButton2Click( wxCommandEvent& event )
{
rrr *dlg = new rrr(NULL,ID_DIALOG);
dlg->ShowModal();
dlg->Destroy();
event.Skip();
}
关闭主窗口

Code: Select all

void dialog5::OnButton1Click( wxCommandEvent& event )
{
Destroy();
event.Skip();
}
关闭子窗口

Code: Select all

void rrr::click2(wxCommandEvent &event)
{
  Destroy();
  event.Skip();
}
加载图片和按钮控件

Code: Select all

void rrr::CreateControls()
{    
rrr* itemDialog2 = this;

itemButton1Bmp.LoadFile(_T("pic/11.jpg"), wxBITMAP_TYPE_JPEG);

itemBitmapButton1 = new wxBitmapButton( itemDialog1, ID_BUTTON1, itemButton1Bmp, wxPoint(100,400),  wxDefaultSize, wxBUTTON_SHAPED|wxNO_BORDER);
}

kingkamg
I live to help wx-kind
I live to help wx-kind
Posts: 187
Joined: Tue Apr 08, 2008 1:45 pm

Post by kingkamg » Mon Jul 07, 2008 1:44 am

你为何要每一个都event.skip()?为何不在你这里就结束呢?

rrr *dlg = new rrr(NULL,ID_DIALOG);
dlg->ShowModal();
dlg->Destroy();
if( dlg )
delete dlg;

这样试试,我没试,单内存泄漏的问题,至少我用wx一年多未发现

bloodlee
Experienced Solver
Experienced Solver
Posts: 77
Joined: Thu Nov 30, 2006 10:49 am
Location: Shenzhen, CHN

Post by bloodlee » Tue Jul 08, 2008 8:50 am

Hi, swallow.
推荐一个很好用的东西, 叫Visual Leak Detector的.
带着它, 先在PC下编译, 查查到底是哪漏了, 再做CE的BINARY.
这里有一篇中文的说明.
http://www.testage.net/AutoTest/Opentest/200609/896.htm

swallow
Earned a small fee
Earned a small fee
Posts: 20
Joined: Thu Jun 19, 2008 12:57 pm

Post by swallow » Mon Jul 14, 2008 4:51 am

我用kingkamg的方法试了,还是不行,回头试试bloodlee的办法,谢谢

Post Reply