gpt4 book ai didi

c - X 工具包 : heap is growing when recreating widgets

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:28 24 4
gpt4 key购买 nike

我试图理解为什么以下程序会泄漏内存。当我使用命令 more /proc/<pid>/smaps 查看堆大小时,我可以看到堆只在增长。似乎 XtDestroyWidget 并没有真正释放内存。如果有人能告诉我为什么会这样并告诉我重新创建小部件的正确方法,我将不胜感激。提前致谢!

/* Compile on Solaris: cc widgets_mem.c -lXm -lXt -lX11 */
/* Compile on Linux: gcc -m32 widgets_mem.c -lXm -lXt -lX11 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysymdef.h>

#include <Xm/MainW.h>
#include <Xm/PushB.h>
#include <Xm/Form.h>


Widget toplevel;
Widget w_main;
Widget w_bb;
Widget w_button = NULL;

Window root_win;
XtAppContext app;
int screen_number;
Display *display = NULL;

void recreateWidgets ()
{
printf ("recreating widgets\n");
XtDestroyWidget (w_button);

w_button = XtVaCreateManagedWidget (
"button",
xmPushButtonWidgetClass, w_bb,
XmNfillOnArm, False,
XmNhighlightThickness, 0,
XmNborderWidth, 1,
XmNmarginTop, 1,
XmNmarginWidth, 2,
XmNmarginHeight, 0,
XmNwidth, 20,
XmNheight, 10,
XmNrecomputeSize, False,
XmNalignment, XmALIGNMENT_CENTER,
NULL);
}


void main (int argc, char **argv)
{
XEvent event;

toplevel = XtVaAppInitialize (&app, "Mem leak test",
NULL, 0, &argc, argv, NULL, NULL);
display = XtDisplay (toplevel);
screen_number = DefaultScreen (display);
root_win = RootWindow (display, screen_number);

w_main = XtVaCreatePopupShell (
"main",
topLevelShellWidgetClass, toplevel,
XmNgeometry, "-0+0",
XmNborderWidth, 0,
XmNshadowThickness, 0,
XmNminWidth, 1,
XmNmwmDecorations, 0,
XmNmwmFunctions, 0,
XmNwidth, 600,
XmNheight, 200,
NULL);

w_bb = XtVaCreateManagedWidget (
"",
xmBulletinBoardWidgetClass, w_main,
XmNborderWidth, 4,
XmNshadowThickness, 0,
XmNmarginWidth, 0,
XmNmarginHeight, 0,
XmNx, 0,
XmNy, 0,
XmNwidth, 500,
XmNheight, 100,
NULL);

w_button = XtVaCreateManagedWidget (
"button",
xmPushButtonWidgetClass, w_bb,
XmNfillOnArm, False,
XmNhighlightThickness, 0,
XmNborderWidth, 1,
XmNmarginTop, 1,
XmNmarginWidth, 2,
XmNmarginHeight, 0,
XmNwidth, 20,
XmNheight, 10,
XmNrecomputeSize, False,
XmNalignment, XmALIGNMENT_CENTER,
NULL);

XtPopup (w_main, XtGrabNone);

while (1)
{
XtAppNextEvent (app, &event);
if (event.type == ButtonPress)
{
printf ("Button pressed, recreating widgets\n");
recreateWidgets();
}
}
}

最佳答案

我在我的代码中发现了错误:我必须添加一个 XtDispatchEvent(&event),它处理 destory 列表上的小部件的重新分配,这是 Xt 中的一个内部列表。

关于c - X 工具包 : heap is growing when recreating widgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45240103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com