Skip to content
This repository
Browse code

Fixing memory leak in ThemeView where __paths would grow each time _p…

…aths() was called. Fixes #329
  • Loading branch information...
commit 46948dd2c046049bae07baeedb62c8fa4cf258f0 1 parent 0ce14ce
Mark Story authored February 11, 2010
3  cake/libs/view/theme.php
@@ -68,8 +68,7 @@ function _paths($plugin = null, $cached = true) {
68 68
 			}
69 69
 			$paths = array_merge($themePaths, $paths);
70 70
 		}
71  
-		$this->__paths = $paths;
72  
-		return $this->__paths;
  71
+		return $paths;
73 72
 	}
74 73
 }
75 74
 ?>
38  cake/tests/cases/libs/view/theme.test.php
@@ -190,8 +190,6 @@ function startTest() {
190 190
 			'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
191 191
 			'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
192 192
 		));
193  
-		$Configure = App::getInstance();
194  
-		array_shift($Configure->views);
195 193
 	}
196 194
 
197 195
 /**
@@ -217,7 +215,7 @@ function testPluginThemedGetTemplate() {
217 215
 		$this->Controller->action = 'index';
218 216
 		$this->Controller->theme = 'test_theme';
219 217
 
220  
-		$ThemeView = new TestThemeView($this->Controller);
  218
+		$ThemeView =& new TestThemeView($this->Controller);
221 219
 		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'tests' . DS .'index.ctp';
222 220
 		$result = $ThemeView->getViewFileName('index');
223 221
 		$this->assertEqual($result, $expected);
@@ -225,7 +223,7 @@ function testPluginThemedGetTemplate() {
225 223
 		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'plugins' . DS . 'test_plugin' . DS . 'layouts' . DS .'plugin_default.ctp';
226 224
 		$result = $ThemeView->getLayoutFileName('plugin_default');
227 225
 		$this->assertEqual($result, $expected);
228  
-		
  226
+
229 227
 		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'layouts' . DS .'default.ctp';
230 228
 		$result = $ThemeView->getLayoutFileName('default');
231 229
 		$this->assertEqual($result, $expected);
@@ -244,7 +242,7 @@ function testGetTemplate() {
244 242
 		$this->Controller->action = 'display';
245 243
 		$this->Controller->params['pass'] = array('home');
246 244
 
247  
-		$ThemeView = new TestThemeView($this->Controller);
  245
+		$ThemeView =& new TestThemeView($this->Controller);
248 246
 		$ThemeView->theme = 'test_theme';
249 247
 		$expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS .'pages' . DS .'home.ctp';
250 248
 		$result = $ThemeView->getViewFileName('home');
@@ -285,7 +283,7 @@ function testMissingView() {
285 283
 		$this->Controller->params['pass'] = array('home');
286 284
 
287 285
 		restore_error_handler();
288  
-		$View = new TestThemeView($this->Controller);
  286
+		$View =& new TestThemeView($this->Controller);
289 287
 		ob_start();
290 288
 		$result = $View->getViewFileName('does_not_exist');
291 289
 		$expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
@@ -308,7 +306,7 @@ function testMissingLayout() {
308 306
 		$this->Controller->theme = 'my_theme';
309 307
 
310 308
 		restore_error_handler();
311  
-		$View = new TestThemeView($this->Controller);
  309
+		$View =& new TestThemeView($this->Controller);
312 310
 		ob_start();
313 311
 		$result = $View->getLayoutFileName();
314 312
 		$expected = str_replace(array("\t", "\r\n", "\n"), "", ob_get_clean());
@@ -316,5 +314,31 @@ function testMissingLayout() {
316 314
 		$this->assertPattern("/Missing Layout/", $expected);
317 315
 		$this->assertPattern("/views(\/|\\\)themed(\/|\\\)my_theme(\/|\\\)layouts(\/|\\\)whatever.ctp/", $expected);
318 316
 	}
  317
+
  318
+/**
  319
+ * test memory leaks that existed in _paths at one point.
  320
+ *
  321
+ * @return void
  322
+ */
  323
+	function testMemoryLeakInPaths() {
  324
+		if ($this->skipIf(function_exists('get_memory_usage'), 'No memory measurement function, fail. %s')) {
  325
+			return;
  326
+		}
  327
+		$this->Controller->plugin = null;
  328
+		$this->Controller->name = 'Posts';
  329
+		$this->Controller->viewPath = 'posts';
  330
+		$this->Controller->layout = 'whatever';
  331
+		$this->Controller->theme = 'test_theme';
  332
+
  333
+		$View =& new ThemeView($this->Controller);
  334
+		$View->element('test_element');
  335
+
  336
+		$start = memory_get_usage();
  337
+		for ($i = 0; $i < 10; $i++) {
  338
+			$View->element('test_element');
  339
+		}
  340
+		$end = memory_get_usage();
  341
+		$this->assertWithinMargin($start, $end, 2000);
  342
+	}
319 343
 }
320 344
 ?>

0 notes on commit 46948dd

Please sign in to comment.
Something went wrong with that request. Please try again.