Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Fixed memory leak in ValidatorBuilder #11454

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

Merged
merged 1 commit into from
Jul 23, 2014

Conversation

webmozart
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #11236
License MIT
Doc PR -

In 23534ca, the following code was introduced in ValidatorBuilder::getValidator():

AnnotationRegistry::registerLoader(function ($class) {
    if (0 === strpos($class, __NAMESPACE__.'\\Constraints\\')) {
        $file = str_replace(__NAMESPACE__.'\\Constraints\\', __DIR__.'/Constraints/', $class).'.php';

        if (is_file($file)) {
            require_once $file;

            return true;
        }
    }

    return false;
});

AnnotationRegistry::registerLoader() stores all loaders in a global array. Every time that getValidator() is called, a new closure is put onto the loader stack, referencing $this (i.e. the ValidatorBuilder) and consequently preventing the ValidatorBuilder (and its references) from being garbage-collected.

The call to registerLoader() did not exist in 2.4 and I can't find a reason why it should be there. All tests are green without that code. I suppose I used it for debugging and then forgot to remove it again, so I'm removing it here.

@fabpot
Copy link
Member

fabpot commented Jul 23, 2014

👍

1 similar comment
@nicolas-grekas
Copy link
Member

👍

@fabpot
Copy link
Member

fabpot commented Jul 23, 2014

Thank you @webmozart.

@fabpot fabpot merged commit 283387a into symfony:2.5 Jul 23, 2014
fabpot added a commit that referenced this pull request Jul 23, 2014
This PR was merged into the 2.5 branch.

Discussion
----------

[Validator] Fixed memory leak in ValidatorBuilder

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11236
| License       | MIT
| Doc PR        | -

In 23534ca, the following code was introduced in `ValidatorBuilder::getValidator()`:

```php
AnnotationRegistry::registerLoader(function ($class) {
    if (0 === strpos($class, __NAMESPACE__.'\\Constraints\\')) {
        $file = str_replace(__NAMESPACE__.'\\Constraints\\', __DIR__.'/Constraints/', $class).'.php';

        if (is_file($file)) {
            require_once $file;

            return true;
        }
    }

    return false;
});
```

`AnnotationRegistry::registerLoader()` stores all loaders in a global array. Every time that `getValidator()` is called, a new closure is put onto the loader stack, referencing `$this` (i.e. the ValidatorBuilder) and consequently preventing the ValidatorBuilder (and its references) from being garbage-collected.

The call to `registerLoader()` did not exist in 2.4 and I can't find a reason why it should be there. All tests are green without that code. I suppose I used it for debugging and then forgot to remove it again, so I'm removing it here.

Commits
-------

283387a [Validator] Fixed memory leak in ValidatorBuilder
@stof
Copy link
Member

stof commented Aug 5, 2014

@webmozart I suppose this code was added to make it easier to use annotations for people not registering their composer autoloader in the AnnotationRegistry. However, no other library I know of are actually doing it, so people using Doctrine annotations already need to be aware of the AnnotationRegistry.

And btw, if you are sure that all your autoloaders are failing gracefully when they cannot autoload a class, it is fine to register class_exists in the AnnotationRegistry. The AnnotationRegistry is here mostly because PHP does not require autoloaders to avoid breaking class_exists and several older implementations are breaking it (Doctrine Common and ZF1 for instance).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants