Skip to content

Commit

Permalink
fixed a memory leak when using validations
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Jun 16, 2010
1 parent 688f0fa commit 46a1219
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Validations.php
Expand Up @@ -136,6 +136,7 @@ public function validate()
foreach ($this->validators as $validate)
$this->$validate($reflection->getStaticPropertyValue($validate));

$this->record->clear_model();
return $this->record;
}

Expand Down Expand Up @@ -643,6 +644,15 @@ public function __construct(Model $model)
$this->model = $model;
}

/**
* Nulls $model so we don't get pesky circular references. $model is only needed during the
* validation process and so can be safely cleared once that is done.
*/
public function clear_model()
{
$this->model = null;
}

/**
* Add an error message.
*
Expand Down
7 changes: 7 additions & 0 deletions test/ValidationsTest.php
Expand Up @@ -114,5 +114,12 @@ public function test_get_validation_rules()
$validators = BookValidations::first()->get_validation_rules();
$this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name']));
}

public function test_model_is_nulled_out_to_prevent_memory_leak()
{
$book = new BookValidations();
$book->is_valid();
$this->assert_true(strpos(serialize($book->errors),'model";N;') !== false);
}
};
?>

0 comments on commit 46a1219

Please sign in to comment.