php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32799 crash: calling the corresponding global var during the destruct
Submitted: 2005-04-22 05:19 UTC Modified: 2005-06-06 12:40 UTC
From: rd dot contact at free dot fr Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-04-25 OS: *
Private report: No CVE-ID: None
 [2005-04-22 05:19 UTC] rd dot contact at free dot fr
Description:
------------
During the __destruct, when using the global var assigned to the object, php crashes in some cases.





Reproduce code:
---------------
class test{
  function __destruct (){
    //$GLOBALS['p']=$this; // <- this crash apache for all tests
    //$GLOBALS['p']++; // <- this crash apache for all tests
    print_r($GLOBALS['p']); // <- crash only test 1. test 2 => "Undefined variable p"
  }
}
$p=new test;

//test 1
$p=1; // crash apache (as $p=null, $p='bug', $p=new test ...)

//but,

//test 2
//unset($p); //  no crash: "Undefined variable:  p on line 5"

Expected result:
----------------
No crash, and a way to use the global var during destruct.
(so destruct could call external functions that use the global var of the object. Why not unregister the global var after the destruct call ?)



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-22 14:31 UTC] sniper@php.net
Can you give a bit more realistic example script?
The one here does not make any sense..

 [2005-04-22 19:34 UTC] rd dot contact at free dot fr
/*
-- TEST 1
   I use the global var instead of "$this" to demonstrate 
   that calling it will segfault php.
   Try TEST2 to see why it could be useful to use the global
   var during the destruct.
   unset($p) instead of $p=[anything], don't crash.
*/
class test{
  public $c=1;
  function __destruct (){
    $GLOBALS['p']->c++; // no warning
    print $GLOBALS['p']->c; // segfault
  }
}
$p=new test;
$p=null; //destroy the object by a new assignment (segfault)

//---CUT----

/*
   -- TEST 2
   More realistic example:
   During the destruct, I need to call an external function 
   that uses the global var of the object.
   calling a methode: works
   incrementing a property: segfault
*/

function dbug($msg){
$GLOBALS['p']->printmsg($msg); // works
$GLOBALS['p']->c++; // segfault
}

class test{
  public $c=1;
  function __destruct (){dbug('Destruct');}
  function printmsg($msg){print $msg;}
}
$p=new test;
$p=null; //destroy the object by a new assignment (segfault)
 [2005-06-06 12:40 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 10:01:29 2024 UTC