Memory problem with bitmaps
Olcay Korol <[email protected]> Wed, 3 Sep 2003 19:32:51 +0300
| Newsgroups | gmane.comp.video.picogui.devel |
|---|---|
| Organization | Eliar A.S. |
| Message-ID | <[email protected]> |
Hi all, I have a problem using bitmaps. I attached a simple piece of code describing that situation. It works on an SA1110-based system with 2.4.9 kernel and with PicoGUI version 0.45. This app puts two buttons on screen. The one the right is used to kill the app. The one on the left, when clicked, opens a popup with a list of menuitems. Each menuitem contains a bitmap image. This menu is closed whenever one of the menuitems is clicked. When i measure free memory in the system (using shell command free), i see that every opening and closing of the menu spends 4kB of memory. No memory is spent when there is no bitmap. I'm very careful with "my own allocations", it is obvious that this loss cannot be due to malloc stuff in my code. It seems that pgLeaveContext frees memory only for widgets and strings, not for bitmaps. It probably only marks the handle as unused, but does not" physically" free the memory. If pgLeaveContext did free that piece of memory, there wouldn't be any memory leak. If not, how can i free that piece of memory when i'm finished with bitmaps? That's very important cause we can have so many open/close operations with menus of this kind in a bigger application. Any help is appreciated, Olcay Korol Department of R&D Eliar A.S.
main.c
(application/octet-stream, 3.3 KB)
/***************************************************************************
main.c - description
-------------------
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <picogui.h>
typedef struct _widgetSet
{
pghandle box0, box1, box2, box3;
pghandle button0, button1;
pghandle *wLabels;
pghandle window;
pghandle wItemNumber;
}widgetSet;
void createMenu(widgetSet *W);
int main(int argc, char *argv[])
{
widgetSet W;
int i, payload;
int flag = 1;
struct pgEvent *event;
W.wLabels = (pghandle *) malloc(sizeof(pghandle)*5);
if(W.wLabels == NULL)
return -1;
pgInit(argc, argv);
pgRegisterApp(PG_APP_NORMAL, "Bitmap Test", 0);
pgEnterContext();
W.box0 = pgNewWidget(PG_WIDGET_BOX, PG_DERIVE_INSIDE, 0);
pgSetWidget(PGDEFAULT,
PG_WP_SIZE, 30,
PG_WP_SIDE, PG_S_TOP,
0);
W.button0 = pgNewWidget(PG_WIDGET_BUTTON, PG_DERIVE_INSIDE, 0);
pgSetWidget(PGDEFAULT,
PG_WP_TEXT, pgNewString("Click Me!"),
PG_WP_SIDE, PG_S_LEFT,
0);
pgSetPayload(PGDEFAULT, 500);
W.button1 = pgNewWidget(PG_WIDGET_BUTTON, PG_DERIVE_AFTER, 0);
pgSetWidget(PGDEFAULT,
PG_WP_TEXT, pgNewString("Kill App!"),
PG_WP_SIDE, PG_S_LEFT,
0);
pgSetPayload(PGDEFAULT, 501);
W.box1 = pgNewWidget(PG_WIDGET_BOX, PG_DERIVE_AFTER, W.box0);
pgSetWidget(PGDEFAULT,
PG_WP_SIDE, PG_S_BOTTOM,
0);
pgUpdate();
pgFocus(W.button0);
do
{
event = pgGetEvent();
if(event->type == PG_WE_FOCUS)
{
}
else if(event->type == PG_WE_ACTIVATE)
{
payload = pgGetPayload(event->from);
switch(payload)
{
case 500 :
createMenu(&W);
pgUpdate();
break;
case 501 :
flag = 0;
break;
default :
break;
}
}
}while(flag);
pgLeaveContext();
free(W.wLabels);
return 1;
}
void createMenu(widgetSet *W)
{
int i, payload;
int flag = 1;
struct pgEvent *event;
char test[15];
pgEnterContext();
pgNewPopupAt(10,100, 120, 80);
for(i=0; i < 5; i++)
{
if(i == 0)
W->wLabels[i] = pgNewWidget(PG_WIDGET_MENUITEM, PG_DERIVE_INSIDE, 0);
else
W->wLabels[i] = pgNewWidget(PG_WIDGET_MENUITEM, PG_DERIVE_AFTER, 0);
sprintf(test, "Menuitem %d", i);
pgSetWidget(PGDEFAULT,
PG_WP_SIDE, PG_S_TOP,
PG_WP_TEXT, pgNewString(test),
PG_WP_FONT, pgNewFont("Times New Roman",8,PG_FSTYLE_DEFAULT),
PG_WP_BITMAP, pgNewBitmap(pgFromFile("alarm.bmp")),
PG_WP_EXTDEVENTS, PG_EXEV_FOCUS,
0);
pgSetPayload(PGDEFAULT, 502 + i);
}
pgUpdate();
do
{
event = pgGetEvent();
if(event->type == PG_WE_FOCUS)
{
}
else if(event->type == PG_WE_ACTIVATE)
{
payload = pgGetPayload(event->from);
switch(payload)
{
case 502 :
case 503 :
case 504 :
case 505 :
case 506 :
flag = 0;
break;
default :
break;
}
}
}while(flag);
pgLeaveContext();
}