Oh My! What have I done !!!

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Oh My! What have I done !!!

Post 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:
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

:P kool, make a story about it
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;) )
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

try freeing variables by using unset($variable);
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post 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.
Post Reply