Skip to content

Commit 46a1219

Browse files
committed
fixed a memory leak when using validations
1 parent 688f0fa commit 46a1219

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/Validations.php

+10
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function validate()
136136
foreach ($this->validators as $validate)
137137
$this->$validate($reflection->getStaticPropertyValue($validate));
138138

139+
$this->record->clear_model();
139140
return $this->record;
140141
}
141142

@@ -643,6 +644,15 @@ public function __construct(Model $model)
643644
$this->model = $model;
644645
}
645646

647+
/**
648+
* Nulls $model so we don't get pesky circular references. $model is only needed during the
649+
* validation process and so can be safely cleared once that is done.
650+
*/
651+
public function clear_model()
652+
{
653+
$this->model = null;
654+
}
655+
646656
/**
647657
* Add an error message.
648658
*

test/ValidationsTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,12 @@ public function test_get_validation_rules()
114114
$validators = BookValidations::first()->get_validation_rules();
115115
$this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name']));
116116
}
117+
118+
public function test_model_is_nulled_out_to_prevent_memory_leak()
119+
{
120+
$book = new BookValidations();
121+
$book->is_valid();
122+
$this->assert_true(strpos(serialize($book->errors),'model";N;') !== false);
123+
}
117124
};
118125
?>

0 commit comments

Comments
 (0)