Page 1 of 1

Oh My! What have I done !!!

Posted: Thu Oct 30, 2003 4:43 pm
by Heavy
This is the first time I've felt so looooser about my code.

Having my OOP structure for retrieving articles out of mysql and outputting one single article returns this:

Code: Select all

Fatal error: Allowed memory size of 8388608 bytes exhausted at (null):0 (tried to allocate 40 bytes) in /var/www/classes/class.iterator.php on line 30

Fatal error: Allowed memory size of 8388608 bytes exhausted at (null):0 (tried to allocate 135 bytes) in Unknown on line 0
I really hope it is just a simple bug and not really a bad bad bad code design.

No way! I won't post it here! :lol:

Posted: Thu Oct 30, 2003 6:57 pm
by Cruzado_Mainfrm
:P kool, make a story about it

Posted: Thu Oct 30, 2003 7:15 pm
by Gen-ik
Try adding mysql_free_result() after your database calls.

Code: Select all

$sql = "SELECT * FROM `myTable`";

$result = mysql_query($sql);

mysql_free_result();
....might help.

Posted: Thu Oct 30, 2003 9:06 pm
by volka
hm, iterator.php...
You're sure there isn't an infinte loop consuming more and more memory? Something like

Code: Select all

<?php
$arr = array('a', 'b');
$arr2 = array();
for($i=0; $i=count($arr); $i++) // must be !=count(...
	$arr2[] = $arr[$i];
?>
would cause such an effect (ok, poor example ;) )

Posted: Thu Oct 30, 2003 9:15 pm
by Cruzado_Mainfrm
try freeing variables by using unset($variable);

Posted: Fri Oct 31, 2003 1:25 am
by Heavy
You all got me wrong :lol:
Yes, I was doing crazy things. But I solved it just after I posted yesterday.

I posted in the General Discussion because I was not asking for help.

Here's how it works now:

Code: Select all

<?php
/*
@descr: Handles looping through an array;
*/
class iterator{
	var $arrResultSet = array();
	var $intIndex = 0;
	var $element = null;
	/**
	* @return void
	* @param $array array
	* @desc Moves index forward $intSteps steps if positive, backwards if negative.
 	*/
	function setResultSet( $array){
		$this->arrResultSet = $array;
		$this->intIndex = 0;
	}
	
	function reset(){
		$this->intIndex = 0;
	}
	
	/**
	* @return mixed
	* @desc Fetches current element in array then moves index forward.
			Returns false when no elements are left to fetch.
 	*/
	function fetchNextElement(){
		if (isset($this->arrResultSet[$this->intIndex])){
			$this->element = & $this->arrResultSet[$this->intIndex];
			$this->intIndex++;
			return true;
		}else{
			$this->reset();
			return false;
		}
	}
	
	/**
	* @return void
	* @param $intSteps integer
	* @desc Moves index forward $intSteps steps if positive, backwards if negative.
 	*/
	function moveIndex($intSteps){
		$this->intIndex += $intSteps;
	}
	
	function getIndex(){
		return $this->intIndex ;
	}
	
	/**
	* @return unknown
	* @desc returns currently fetched element.	
 */
	function & getElement(){
		return $this->element;
	}

	function & getAllElements(){
		return $this->arrResultSet;
	}
}
?>
I wanted a way to do this:

Code: Select all

<?php
		//$this is of class article that has inherited the iterator class.
		while($this->fetchNextElement()){
			$arrArticle = & $this->getElement();
			$arrArticle['strDate'] = date('Y-m-d H:i', $arrArticle['intTimeStamp']);
		}
?>
It works just as intended because of the reference passing all over the place.

I shot myself in my foot while setting this up and was surprised, because I had never seen this error msg before.
Even more fun, was that I was only pulling like 100 bytes out of mysql while getting an 8MB memory limit fatal error :lol:

I didn't really need help this time, but I'd like to say thanks to volka anyway, because he/she has been of unmatched help before when I've been in trouble.

Cheers.