From 07ac028558aa1059d4561e7ce0bd92f9bc3f10c9 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sat, 12 Jan 2013 18:06:31 +0100 Subject: [PATCH] Fixed a mini memory leak --- src/API/ConsoleAPI.php | 5 +++++ src/classes/Player.class.php | 25 ++++++++++++--------- src/classes/PocketMinecraftServer.class.php | 6 ++--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 95db7c427..ee8c59835 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -49,6 +49,11 @@ class ConsoleAPI{ public function defaultCommands($cmd, $params){ switch($cmd){ + case "crash": //Crashes the server to generate an report + $this->callNotDefinedMethodCrash(); + $this->server->api->server; //Access a private property + callNotExistingFunction(); + break; case "invisible": $p = strtolower(array_shift($params)); switch($p){ diff --git a/src/classes/Player.class.php b/src/classes/Player.class.php index 0cd46385b..54ebf1e3c 100644 --- a/src/classes/Player.class.php +++ b/src/classes/Player.class.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or class Player{ - private $server, $timeout, $connected, $queue, $buffer; + private $server, $timeout, $connected, $queue, $buffer, $evid = array(); var $clientID, $ip, $port, $counter, $username, $eid, $data, $entity, $auth, $CID, $MTU, $spawned, $equipment; function __construct($server, $clientID, $ip, $port, $MTU){ $this->queue = array(); @@ -44,8 +44,8 @@ class Player{ $this->timeout = microtime(true) + 25; $this->equipment = array(1, 0); $this->spawned = false; - $this->server->event("server.tick", array($this, "onTick")); - $this->server->event("server.close", array($this, "close")); + $this->evid[] = $this->server->event("server.tick", array($this, "onTick")); + $this->evid[] = $this->server->event("server.close", array($this, "close")); console("[DEBUG] New Session started with ".$ip.":".$port.". MTU ".$this->MTU.", Client ID ".$this->clientID, true, true, 2); $this->connected = true; $this->auth = false; @@ -92,6 +92,9 @@ class Player{ public function close($reason = "", $msg = true){ if($this->connected === true){ + foreach($this->evid as $ev){ + $this->server->deleteEvent($ev); + } $this->server->api->dhandle("player.quit", $this); $reason = $reason == "" ? "server stop":$reason; $this->save(); @@ -323,14 +326,14 @@ class Player{ $this->entity->data["clientID"] = $this->clientID; $this->server->api->entity->spawnAll($this); $this->server->api->entity->spawnToAll($this->eid); - $this->server->event("server.timee", array($this, "eventHandler")); - $this->server->event("server.chat", array($this, "eventHandler")); - $this->server->event("entity.remove", array($this, "eventHandler")); - $this->server->event("entity.move", array($this, "eventHandler")); - $this->server->event("entity.animate", array($this, "eventHandler")); - $this->server->event("player.equipment.change", array($this, "eventHandler")); - $this->server->event("player.pickup", array($this, "eventHandler")); - $this->server->event("block.change", array($this, "eventHandler")); + $this->evid[] = $this->server->event("server.timee", array($this, "eventHandler")); + $this->evid[] = $this->server->event("server.chat", array($this, "eventHandler")); + $this->evid[] = $this->server->event("entity.remove", array($this, "eventHandler")); + $this->evid[] = $this->server->event("entity.move", array($this, "eventHandler")); + $this->evid[] = $this->server->event("entity.animate", array($this, "eventHandler")); + $this->evid[] = $this->server->event("player.equipment.change", array($this, "eventHandler")); + $this->evid[] = $this->server->event("player.pickup", array($this, "eventHandler")); + $this->evid[] = $this->server->event("block.change", array($this, "eventHandler")); console("[DEBUG] Player with EID ".$this->eid." \"".$this->username."\" spawned!", true, true, 2); $this->eventHandler(new Container($this->server->motd), "server.chat"); diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 16e1c8712..1d66b33ac 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -541,7 +541,7 @@ class PocketMinecraftServer{ $add = ' unset($this->schedule['.$this->scheduleCnt.']);'; } $this->schedule[$this->scheduleCnt] = array($callback, $data, $eventName); - $this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.'];'.$add.'if(!is_callable($schedule[0])){unset($this->schedule['.$this->scheduleCnt.']);return;} call_user_func($schedule[0], $schedule[1], $schedule[2]);', (bool) $repeat); + $this->action(50000 * $ticks, '$schedule = $this->schedule['.$this->scheduleCnt.'];'.$add.'if(!is_callable($schedule[0])){unset($this->schedule['.$this->scheduleCnt.']);return false;} call_user_func($schedule[0], $schedule[1], $schedule[2]);', (bool) $repeat); return $this->scheduleCnt++; } @@ -561,8 +561,8 @@ class PocketMinecraftServer{ return; } while(false !== ($action = $actions->fetchArray(SQLITE3_ASSOC))){ - eval(base64_decode($action["code"])); - if($action["repeat"] === 0){ + $return = eval(base64_decode($action["code"])); + if($action["repeat"] === 0 or $return === false){ $this->query("DELETE FROM actions WHERE ID = ".$action["ID"].";"); } }