Re: Changing font colors on the fly == memory leak!
| From: | Brian Stumm | Date: | Tue, 03 Jan 2006 15:12:27 +0000 |
| Subject: | Re: Changing font colors on the fly == memory leak! | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-6496@lists.php.net to get a copy of this message | ||
Nghia wrote:
> -----Original Message-----
> From: Brian Stumm [mailto:bstumm@energytechnologysystems.com]
> Sent: Friday, 30 December 2005 7:22 AM
> To: php-gtk-general@lists.php.net
> Subject: [PHP-GTK] Changing font colors on the fly == memory leak!
>
> I must be doing this wrong. My method results in a memory leak. Below is
> some output from ps aux under linux showing the memory usage climbing
> steadily. Below that is a sample app demonstrating the memory leak
> I posted a similar problem of memory leaking a while ago. I think the
> consesus is that php4 doesn't free any memory until the script ends. This
> might be fixed in the php gtk2. You can try and minimize the problem by
> declaring objects once globally, and perhaps restart your process
> periodically if possible.
I figured out the problem, below is the updated version of previous script
with no memory leaking.
<?php
/* Load php-gtk library */
if (!extension_loaded('gtk')) { // Load the GTK Extension
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
###############
## Functions ##
###############
/***** DELETE_EVENT() *****/
// Called when delete-event happens. Returns false to indicate that the
event should proceed.
function delete_event() {
return false;
}
/***** DESTROY() *****/
// Called when the window is being destroyed. Simply quit the main loop.
function destroy() {
global $fp;
if($fp) {
fclose ($fp);
}
while(Gtk::events_pending()) {
Gtk::main_iteration();
}
Gtk::main_quit();
die();
}
/***** UPDATESCREEN() *****/
function UpdateScreen() {
while(Gtk::events_pending()) {
Gtk::main_iteration();
}
return true;
}
/***** MAIN() *****/
function main() {
global $z1t, $st1, $style_yellow, $style_green;
$num = RAND(1,10);
if($num < 5) {
$z1t->set_style($style_yellow);
} else {
$z1t->set_style($style_green);
}
$z1t->set_text($num);
UpdateScreen();
return true;
}
#################
## Main Window ##
#################
$green=&new gdkcolor('darkgreen');
$yellow=&new gdkcolor('darkgoldenrod');
$window = &new GtkWindow();
$window->connect('destroy', 'destroy');
$window->connect('delete-event', 'delete_event');
$window->set_policy(0, 0, 0);
$window->set_border_width(0);
$window->set_title($window_title);
$window->set_name('MainWindow');
$window->set_usize(400,300);
$window->set_position(GTK_WIN_POS_CENTER);
$window->realize();
$layout = &new GtkLayout(null,null);
$window->add($layout);
// Box Container - Absolute Positioning
$box = &new GtkFixed();
$box->set_border_width(10);
$layout->put($box,0,0);
$box->show();
$z1t = &new GtkLabel('');
$style_yellow = $z1t->style;
$style_green = $z1t->style;
$style_yellow = $style_yellow->copy();
$style_green = $style_green->copy();
$style_yellow->fg[0]=$yellow;
$style_green->fg[0]=$green;
$box->put($z1t,10,10);
$window->show_all();
###############################
## Hand off to GTK Main Loop ##
###############################
// register our main loop/function so it gets processed each 50ms.
gtk::timeout_add(50, 'main');
gtk::main_iteration(); // one refresh
Gtk::main(); // Away we go weeee!
?>