使用 destroy() 命令时的 Perl/Tk 内存泄漏 [英] Perl/Tk Memory Leak When Using destroy() Command

查看:139
本文介绍了使用 destroy() 命令时的 Perl/Tk 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了在 Unix 上运行的 Perl/Tk 中大量泄漏的问题.由于某些限制,我无法发布大部分代码,但我已经能够确定导致内存泄漏的原因并创建了一个具有相同症状的小程序.基本上我正在运行一个 GUI,其中一个框架被定期销毁并重新填充.然而,这似乎会导致大量内存泄漏,并且不断增长.我以为 destroy 命令会清除内存中的所有痕迹,但似乎并非如此.我对 Perl 中的垃圾收集也不太熟悉.该程序将一次运行数天或数周,因此内存泄漏并不理想.有没有人有任何想法来阻止内存泄漏?我只能使用 Perl,所以不幸的是,我不能轻易地将代码移植到 Python 或其他东西.提前感谢您的帮助.

I am having an issue with a large leak in Perl/Tk running on Unix. I am unable to post most of the code due to certain restrictions, but I have been able to identify what is causing the memory leak and create a small program that has the same symptoms. Basically I am running a GUI where a frame is destroyed and repopulated regularly. This seems to cause a large memory leak that keeps growing however. I thought the destroy command would get rid of all traces in memory, but it does not seem to be that way. I am not too familiar with the garbage collection in Perl either. This program will be running for days or weeks at a time and so a memory leak is not ideal. Does anyone have any ideas to stop the memory leak? I am restricted to using Perl, so unfortunately I cannot just easily port the code to Python or something. Thanks for help in advance.

#!opt/project/COTS/bin/perl
use strict;
use warnings;
use Tk;

$Tk::mw = MainWindow->new;
$Tk::mw->repeat(10,\$build);
my $status;
&build;

sub build{
    $status->destroy() if ($status);
    $status = $Tk::mw->Frame->pack(-side => 'top');

    my $left_frame = $status->Frame(
        -relief =>'sunken',
        -borderwidth => 1
    )->pack(-side => 'left', -anchor => 'nw', -padx => 4, -pady => 5);

    my $right_frame = $status->Frame(
        -relief =>'sunken',
        -borderwidth => 1
    )->pack(-side => 'left', -anchor => 'nw', -padx => 4, -pady => 5);

}
MainLoop;

推荐答案

是的,这是众所周知的.Perl/Tk 系统在销毁widget 时会留下一些残留物(实际上不是Perl 的错,而是Perl/Tk 子系统的错).

Yes, this is known. There are some leftovers in the Perl/Tk system when destroying a widget (actually it's not the fault of Perl, but of the Perl/Tk subsystem).

最好的方法是重用小部件.大多数情况下这是可能的,因为您可以重新配置所有选项.如果某些 subwidget 即将消失并重新出现,那么您可以使用 packForget() 暂时将它们从显示中移除,然后再次使用 pack() 使它们可见.

The best approach is to reuse widgets. Most of the time this is possible, as you can re-configure all options. If some subwidgets are about to vanish and reappear, then you can use packForget() to temporarily remove them from display, and use pack() again to make them visible.

这篇关于使用 destroy() 命令时的 Perl/Tk 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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