6.0.0-alpha12
6/7/25

[#6837] Singletons and references
Summary Singletons and references
Queue Horde Framework Packages
Queue Version HEAD
Type Bug
State Resolved
Priority 1. Low
Owners chuck (at) horde (dot) org
Requester thomas.jarosch (at) intra2net (dot) com
Created 06/04/2008 (6212 days ago)
Due
Updated 06/07/2008 (6209 days ago)
Assigned 06/04/2008 (6212 days ago)
Resolved 06/07/2008 (6209 days ago)
Github Issue Link
Github Pull Request
Milestone
Patch No

History
06/07/2008 04:00:29 PM Chuck Hagenbuch Comment #24
Assigned to Chuck Hagenbuch
Taken from Thomas Jarosch
State ⇒ Resolved
Reply to this comment
Looks like it worked when Log::singleton was assigned to a static 
before, and only broke when we removed the static and didn't add the &.
06/06/2008 09:21:23 AM Thomas Jarosch Comment #22 Reply to this comment
This last patch is a bit confusing, since some places you're adding
references too.
Yes, this fixes the factory/singleton code for PHP4. You can drop that 
part if you want as I switched to PHP5 and PHP4 is out of maintenance 
(->doomed) anyway.

I only added it in places where the factory and singleton code is 
already defined as "function &factory()" and "function &singleton()", 
so it should make no difference. I'm really happy they changed the 
default "copy" behavior in PHP5 :-)



Regarding the logger change: The $logger variable is defined as a 
normal "static" var, so the code looked wrong to me. Do you have a 
test case where it failed in the past? Normal logging f.e. of IMAP 
logins still works for me with the new code.


06/06/2008 04:36:00 AM Chuck Hagenbuch Comment #21 Reply to this comment
This last patch is a bit confusing, since some places you're adding 
references too. Also, please be very very careful with that change to 
Horde::getLogger - we've had problems before when the shutdown 
function of PEAR_Log doesn't run, and nothing gets logged.
06/06/2008 04:31:58 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:31:49 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:31:42 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:31:36 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:31:32 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:31:26 AM Chuck Hagenbuch Deleted Original Message
 
06/06/2008 04:18:01 AM Chuck Hagenbuch Deleted Original Message
 
06/05/2008 04:17:17 PM Thomas Jarosch Comment #18
New Attachment: framework-fix-singletons.patch Download
Reply to this comment
Most important patch: Fix singletons in framework.



Please check the changes in Group/Group.php and Kolab/IMAP.php.

Those were the most "complex" ones.



Enjoy :-)


06/05/2008 04:15:00 PM Thomas Jarosch Comment #17
New Attachment: framework-simplify-singletons.patch
Reply to this comment
Simplify patch for framework
06/05/2008 04:14:10 PM Thomas Jarosch Comment #16
New Attachment: imp-simplify-singletons.patch
Reply to this comment
Simplify patch for imp.


06/05/2008 04:13:42 PM Thomas Jarosch Comment #15
New Attachment: kronolith-simplify-singletons.patch
Reply to this comment
Simplify patch for kronolith.
06/05/2008 04:13:14 PM Thomas Jarosch Comment #14
New Attachment: mnemo-simplify-singleton.patch
Reply to this comment
Simplify patch for mnemo.


06/05/2008 04:12:48 PM Thomas Jarosch Comment #13
New Attachment: nag-simplify-singleton.patch
Reply to this comment
Simplify patch for nag.


06/05/2008 04:12:03 PM Thomas Jarosch Comment #12
New Attachment: turba-fix-singletons.patch
Reply to this comment
Fix singleton in Turba and also intitialize three variables properly.




06/05/2008 04:10:47 PM Thomas Jarosch Comment #11
New Attachment: horde-fix-singletons.patch
Reply to this comment
Yipeeee, my segfaults are gone :-)



I've splitted the modifications into patches that really fix things 
and patches that are simplifying the singleton code.



Let's start with the patch for "horde".


06/05/2008 09:37:23 AM Thomas Jarosch Comment #10 Reply to this comment
Your upstream bug contains an incorrect statement:
Fixed upstream ;-)


06/05/2008 09:05:48 AM thomas (dot) jarosch (at) intra2net (dot) com Comment #9 Reply to this comment
circular references patch will not be implemented in PHP 5.3 because 
it did not have many votes.. See: 
http://news.php.net/php.internals/32330.
http://bugs.php.net/bug.php?id=33595 states it's fixed in PHP5_3. Hmm.


06/05/2008 09:00:14 AM Duck Comment #8 Reply to this comment
PHP 5.2.x currently doesn't handle cleanup of cyclic references. PHP
5.3.x should be fixed, but it doesn't work properly.
circular references patch will not be implemented in PHP 5.3 because 
it did not have many votes.. See: 
http://news.php.net/php.internals/32330. You should clean up 
references by your own with a destroy function.  Adding this code into 
the "magic" __destruct() method is impossible because is called when 
an object can be destroyed – as is still referenced it means never.
06/05/2008 08:38:33 AM Thomas Jarosch Comment #7 Reply to this comment
Your upstream bug contains an incorrect statement:
The supplied code is the standard way Horde does it singletons. We
always used the syntax of "$object = &new class" to make it work
with PHP4 and PHP5.
Yeah I know, sorry, I wrote that comment before I learned more 
information as I just checked Horde_History, Group and Perms. It was 
not easy to find the actual problem as it's close to #5355, but not 
100% related.


06/05/2008 08:24:42 AM Michael Slusarz Comment #6 Reply to this comment
PHP 5.2.x currently doesn't handle cleanup of cyclic references. PHP
5.3.x should be fixed, but it doesn't work properly. I filed an
upstream bug: http://bugs.php.net/?id=45178
Your upstream bug contains an incorrect statement:

The supplied code is the standard way Horde does it singletons. We 
always used the syntax of "$object = &new class" to make it work  with 
PHP4 and PHP5.



As Chuck mentioned, the code you provided in that bug report is most 
definitely *not* the "standard" syntax we use.  If that syntax does 
appear anywhere, it is an error and needs to be fixed (thus this bug 
report).  Regardless of whether PHP is leaking memory or not, this 
needs to be cleaned up regardless if just because semantically it is 
incorrect.
06/05/2008 08:07:31 AM Thomas Jarosch Comment #5 Reply to this comment
Actually, I don't think this has anything to do with what you see,
because the PHP manual  explicitly refers to Zend Engine 1.
Everything seems to run smooth until one of those classes created by 
our "wrong" singleton contains an object like PEAR_Error. My 
Horde_History object does this as I don't use a SQL backend. 
PEAR_Error contains cyclic references internally.



PHP 5.2.x currently doesn't handle cleanup of cyclic references. PHP 
5.3.x should be fixed, but it doesn't work properly. I filed an 
upstream bug: http://bugs.php.net/?id=45178



My feeling tells me that something with all those (cyclic) references 
goes wrong during cleanup/reference counting/whatever. Let's see what 
happens after I have fixed the singletons.


06/04/2008 09:41:24 PM Jan Schneider Comment #4 Reply to this comment
Actually, I don't think this has anything to do with what you see, 
because the PHP manual  explicitly refers to Zend Engine 1.

Beside that, what Chuck said.
06/04/2008 09:32:30 PM Chuck Hagenbuch Comment #3
Assigned to Thomas Jarosch
Taken from Horde DevelopersHorde Developers
Reply to this comment
Taking the liberty of assigning to Thomas - thanks for looking in to this!
06/04/2008 07:15:55 PM Chuck Hagenbuch Comment #2
State ⇒ Assigned
Assigned to Horde DevelopersHorde Developers
Reply to this comment
That is correct - we should never assign a reference to a static 
variable. Anywhere this crept in is an error.
06/04/2008 07:09:59 PM thomas (dot) jarosch (at) intra2net (dot) com Comment #1
Priority ⇒ 1. Low
State ⇒ Unconfirmed
Patch ⇒ No
Milestone ⇒
Summary ⇒ Singletons and references
Type ⇒ Bug
Queue ⇒ Horde Framework Packages
Reply to this comment
Hello together,



I'm currently debugging a heap memory corruption in PHP (5.2.6) using Horde

and noticed some odd behavior regarding references and static variables.

Currently we implement some of our singletons like this:



-----------------------------

function &singleton()

{

     static $history;



     if (!isset($history)) {

         $history = &new Horde_History();

     }



     return $history;

}

-----------------------------



I've found out that the singleton is not working and it's related to this:

http://www.php.net/manual/en/language.variables.scope.php

The part with "References with global and static variables" is important.



-> The code has to be changed to "$history = new Horde_History();"

Singletons storing their references via an array are unaffected.



If there a no objections I'll start fixing the static variables.



I really hope this will solve my trouble (=segfaults) deep inside

the PHP memory manager during request shutdown. Spending two days

in gdb, debugging internal PHP memory structures is no fun at all :o)



Cheers,

Thomas


Saved Queries