Skip to content

Memory leak in ArrayIterator when manually calling __construct() #8366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TimWolla opened this issue Apr 13, 2022 · 1 comment
Closed

Memory leak in ArrayIterator when manually calling __construct() #8366

TimWolla opened this issue Apr 13, 2022 · 1 comment

Comments

@TimWolla
Copy link
Member

Description

When running the following code:

<?php

$it = new \ArrayIterator();
$it->next();
$it->__construct([]);

PHP will report a memory leak ("Warning: Leaked 1 hashtable iterators in Unknown on line 0"):

==271560== Memcheck, a memory error detector
==271560== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==271560== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==271560== Command: sapi/cli/php test.php
==271560== 

Warning: Leaked 1 hashtable iterators in Unknown on line 0
==271560== 
==271560== HEAP SUMMARY:
==271560==     in use at exit: 0 bytes in 0 blocks
==271560==   total heap usage: 14,371 allocs, 14,371 frees, 2,141,349 bytes allocated
==271560== 
==271560== All heap blocks were freed -- no leaks are possible
==271560== 
==271560== For lists of detected and suppressed errors, rerun with: -s
==271560== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

PHP Version

PHP 8.2.0-dev (commit 8233e06)

Operating System

Ubuntu 20.04

@morrisonlevi
Copy link
Contributor

Ugh, IMO we need to emit warnings or exceptions when rewind() has been skipped for most built-in iterators -- this would prevent at least part of this bug from happening.

cmb69 added a commit to cmb69/php-src that referenced this issue Apr 14, 2022
When we detach an iterator, we must also have to delete it.
@cmb69 cmb69 linked a pull request Apr 14, 2022 that will close this issue
cmb69 added a commit that referenced this issue Apr 15, 2022
* PHP-8.0:
  Fix GH-8366: ArrayIterator may leak when calling __construct()
@cmb69 cmb69 closed this as completed in 1762a87 Apr 15, 2022
cmb69 added a commit that referenced this issue Apr 15, 2022
* PHP-8.1:
  Fix GH-8366: ArrayIterator may leak when calling __construct()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@TimWolla @morrisonlevi @cmb69 and others