Permalink
Browse files

fixed a memory leak when using validations

  • Loading branch information...
kla committed Jun 16, 2010
1 parent 688f0fa commit 46a121936540102c94ef6d07ab9385e573731f54
Showing with 17 additions and 0 deletions.
  1. +10 −0 lib/Validations.php
  2. +7 −0 test/ValidationsTest.php
View
@@ -136,6 +136,7 @@ public function validate()
foreach ($this->validators as $validate)
$this->$validate($reflection->getStaticPropertyValue($validate));
+ $this->record->clear_model();
return $this->record;
}
@@ -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.
*
View
@@ -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.