Skip to content

Commit

Permalink
[doctrineGH-8410] Fix memory leak in new toIterable and state bug.
Browse files Browse the repository at this point in the history
The new AbstractQuery::toIterable() had a memory leak that
AbstractQuery::iterable() did not have. This leak is now fixed.

After fixing the leak, one test failed where the identity map in
ObjectHydrator triggered and lead to a notice. Introduced a new
AbstractHydrator::cleanupAfterRowIteration() that the ObjectHydrator
uses to cleanup the state.
  • Loading branch information
beberlei committed Feb 8, 2021
1 parent 792a9a9 commit d6b5d70
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ public function toIterable(Statement $stmt, ResultSetMapping $resultSetMapping,

$this->prepare();

$result = [];

while (true) {
$row = $this->_stmt->fetch(FetchMode::ASSOCIATIVE);

Expand All @@ -176,8 +174,12 @@ public function toIterable(Statement $stmt, ResultSetMapping $resultSetMapping,
break;
}

$result = [];

$this->hydrateRowData($row, $result);

$this->cleanupAfterRowIteration();

yield end($result);
}
}
Expand Down Expand Up @@ -274,6 +276,10 @@ protected function cleanup()
->removeEventListener([Events::onClear], $this);
}

protected function cleanupAfterRowIteration(): void
{
}

/**
* Hydrates a single row from the current statement instance.
*
Expand Down
8 changes: 8 additions & 0 deletions lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ protected function cleanup()
$this->_uow->hydrationComplete();
}

protected function cleanupAfterRowIteration(): void
{
$this->identifierMap =
$this->initializedCollections =
$this->existingCollections =
$this->resultPointers = [];
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ public function testNonUniqueObjectHydrationDuringIteration(): void
$bs = IterableTester::iterableToArray(
$q->toIterable([], AbstractQuery::HYDRATE_OBJECT)
);

$this->assertCount(2, $bs);
$this->assertInstanceOf(GH7496EntityB::class, $bs[0]);
$this->assertInstanceOf(GH7496EntityB::class, $bs[1]);
$this->assertEquals(1, $bs[0]->id);
$this->assertEquals(1, $bs[1]->id);

$bs = IterableTester::iterableToArray(
$q->toIterable([], AbstractQuery::HYDRATE_ARRAY)
);

$this->assertCount(2, $bs);
$this->assertEquals(1, $bs[0]['id']);
$this->assertEquals(1, $bs[1]['id']);
}
}

Expand Down

0 comments on commit d6b5d70

Please sign in to comment.