php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63917 SplObjectStorage: Iteration of all the objects is not possible after detach()
Submitted: 2013-01-06 11:25 UTC Modified: 2013-09-21 22:58 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: LastDragon at yandex dot ru Assigned:
Status: Duplicate Package: SPL related
PHP Version: 5.4.10 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: LastDragon at yandex dot ru
New email:
PHP Version: OS:

 

 [2013-01-06 11:25 UTC] LastDragon at yandex dot ru
Description:
------------
Iteration of all the objects is not possible after the detach method was called.

Test script:
---------------
<?php
$s = new SplObjectStorage();

$s->attach(new StdClass);
$s->attach(new StdClass);
$s->attach(new StdClass);
$s->attach(new StdClass);

var_dump($s->count());

foreach ($s as $v) {
    var_dump($v);
    
    $s->detach($v);
}

var_dump($s->count());

Expected result:
----------------
int(4)
object(stdClass)#2 (0) {
}
object(stdClass)#4 (0) {
}
object(stdClass)#5 (0) {
}
int(1)


Actual result:
--------------
int(4)
object(stdClass)#2 (0) {
}
object(stdClass)#3 (0) {
}
object(stdClass)#4 (0) {
}
object(stdClass)#5 (0) {
}
int(0)


Patches

bug63917.phpt (last revision 2013-01-09 15:18 UTC by ashnazg@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-08 21:05 UTC] ashnazg@php.net
Are the "Expected" and "Actual" blocks reversed on this initial report?  I came searching for an SplObjectStorage bug where attempting to foreach() it and detach all its members was failing to detach the final member.  This bug report looks like it reports exactly that, if you switch the Expected and Actual blocks.

In my case, a foreach() around one SplObjectStorage object that performs a detach() per member is finishing the foreach() loop while leaving one member still attached.  I can do a second foreach() on the SplObjectStorage object, and this time it does see the remaining one member and detaches it successfully.  I suspect, as this bug report seems to specifically say, that the detach() operation in the foreach() is causing an off-by-one error or is implicitly calling one of the Iterator methods in a way that throws off the foreach()'s Iterator steps.
 [2013-01-09 06:05 UTC] LastDragon at yandex dot ru
> Are the "Expected" and "Actual" blocks reversed on this initial report?

Yes. Sorry. Unfortunately I cannot edit this blocks :(
 [2013-01-09 15:18 UTC] ashnazg@php.net
The following patch has been added/updated:

Patch Name: bug63917.phpt
Revision:   1357744702
URL:        https://bugs.php.net/patch-display.php?bug=63917&patch=bug63917.phpt&revision=1357744702
 [2013-09-21 22:58 UTC] levim@php.net
-Status: Open +Status: Duplicate
 [2013-09-21 22:58 UTC] levim@php.net
This is duplicate of bug #65629 (https://bugs.php.net/bug.php?id=65629).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 23:01:29 2024 UTC