Skip to content
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

ForeignKeyConstraint constructor doesn't initialize the object completely #4506

Closed
morozov opened this issue Feb 24, 2021 · 5 comments
Closed

Comments

@morozov
Copy link
Member

morozov commented Feb 24, 2021

Bug Report

Q A
Version 2.12.1
<?php

use Doctrine\DBAL\Schema\ForeignKeyConstraint;

$fk = new ForeignKeyConstraint(['user_id'], 'users', ['id']);
$fk->getLocalTableName();

Produces:

Fatal error: Uncaught Error: Call to a member function getName() on null in lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php on line 113

Error: Call to a member function getName() on null in lib/Doctrine/DBAL/Schema/ForeignKeyConstraint.php on line 113

Call Stack:
    0.0005     464176   1. {main}() test.php:0
    0.0133    3261400   2. Doctrine\DBAL\Schema\ForeignKeyConstraint->getLocalTableName() test.php:6
@morozov
Copy link
Member Author

morozov commented Feb 27, 2021

But once the object is completely initialized, there are potential memory leaks:

  1. $constraint->setLocalTable($this);
    if (strlen($constraint->getName())) {
    $name = $constraint->getName();
    } else {
    $name = $this->_generateIdentifierName(
    array_merge((array) $this->getName(), $constraint->getLocalColumns()),
    'fk',
    $this->_getMaxIdentifierLength()
    );
    }
    $name = $this->normalizeIdentifier($name);
    $this->_fkConstraints[$name] = $constraint;
  2. $this->_fkConstraints[$k]->setLocalTable($this);

@greg0ire
Copy link
Member

greg0ire commented Aug 3, 2021

Sorry, can you please explain how the memleak(s) work? I don't get it.

@morozov
Copy link
Member Author

morozov commented Aug 3, 2021

There is a circular reference between the Table and its ForeignKeyConstraint's: the table contains a collection of foreign keys, each of the foreign keys points back to its table. When the local variables referencing pointing to these objects get unset, there are still references from the FKs to the table, so from the garbage collector's standpoint, the table refcount is greater than zero, so the graph doesn't need to be destructed.

I didn't dig into this specific issue deeply but in #4515, the same relationship between Connection and SchemaManager/ExpressionBuilder does lead to connection leaks.

@morozov
Copy link
Member Author

morozov commented Sep 12, 2021

Closed via #4742, #4743.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants