php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71366 When use anonymous function in class property have a memory leak
Submitted: 2016-01-14 09:22 UTC Modified: 2016-01-15 06:02 UTC
From: xiemaomao520 at 163 dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.2 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: xiemaomao520 at 163 dot com
New email:
PHP Version: OS:

 

 [2016-01-14 09:22 UTC] xiemaomao520 at 163 dot com
Description:
------------
Affect all PHP versions.


Test script:
---------------
<?php

class A {

    public function __construct () 
    {
        $this->call = function () {
        };
    }

    public function __destruct() 
    {
        echo 'Destruct: ' . get_class($this) . PHP_EOL;
    }
}

class B 
{
	public function __destruct() 
    {
        echo 'Destruct: ' . get_class($this) . PHP_EOL;
    }
}

function test() 
{
    new A;
	new B;
}

test();

echo "Exit\n";


Expected result:
----------------
Destruct: A
Destruct: B
Exit

Actual result:
--------------
Destruct: B
Exit
Destruct: A

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-14 09:24 UTC] xiemaomao520 at 163 dot com
-Summary: When use anonymous function in class property has leak memory +Summary: When use anonymous function in class property have a memory leak
 [2016-01-14 09:24 UTC] xiemaomao520 at 163 dot com
Change title.
 [2016-01-15 06:02 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug -Package: PDO Core +Package: Scripting Engine problem
 [2016-01-15 06:02 UTC] bwoebi@php.net
That's not a leak, you just are hitting a case in class A which holds a reference to itself via the anonymous function and thus will only be destructed when the cycle collector is run. (Which, in this simple script, is at the end of it.)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 02:01:35 2024 UTC