Skip to content

Memory leaks while changing form many times #200

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
burdiuz opened this issue Jan 6, 2015 · 4 comments
Closed

Memory leaks while changing form many times #200

burdiuz opened this issue Jan 6, 2015 · 4 comments

Comments

@burdiuz
Copy link

burdiuz commented Jan 6, 2015

I was investigating issue
#198
and found that removed form element scopes and controllers continue to live and they listen to events and can break form validation. I've tested this and found that amount of living form element scopes living is proportional to form changes count.
So I've tried to $destroy() scopes of form elements before replacing them with new ones and it fixed both issues this and issue with validation(#198). I guess it would be nice to have ability at least manually destroy them via schema-form API.

Here are part of code resolving the issue

  function Controller($scope, $element, service){
    $scope.addressSchema = {};
    $scope.addressForm = ["*"];
    $scope.$watch('isoCountry', function(value){ // country code changed
        service.loadForm(value).then(function (data) { // load new form json 
            $element.find('input,select,textarea').each(function (index, element){ // find all children inputs
              var childScope = angular.element(element).scope();
              if(childScope){
                childScope.$destroy(); // destroy all children input scopes
              }
            });
            ... Apply new form data here
....
@Dervisevic
Copy link
Contributor

This looks serious, I'll escalate this to @davidlgj , and see if we can come to the same conclusion and any possible solutions.

@davidlgj
Copy link
Contributor

davidlgj commented Jan 8, 2015

hi @burdiuz, and thank you for reporting this. Ugh, not nice at all. So the problem is/was that we use $compile directly in the schema-form directive, instead of the usual templating built in into directives. That in turn means that when we redraw the form it's our responsibility to actually destroy all child scopes.

I did a fix where I create a new scope and destroy that on redraw, this fixes my testcase that reproduces the error, but if you have time to test it and verify that it also works for you that would be awesome. It's pushed to the development branch, so just checkout the latest there. (this is the commit with the fix if you're interested 60c9b72)

@pflorian91
Copy link

snipp
I have the same problem.
The test case can be followed on your example:

  1. Go to http://textalk.github.io/angular-schema-form/examples/bootstrap-example.html
  2. Complete the simple example and submit it > valid
  3. Choose Kitchen Sink example and submit it without completing any field
  4. Choose back the simple example, complete it and submit
    Notice that the fields remain valid, but the form as a whole is not valid.

I am using the library in a similar case.

Thanks!

@davidlgj
Copy link
Contributor

davidlgj commented Jan 8, 2015

@pflorian91 thanks! Nice write up :-)

So I can confirm that my fix, that is in the develop branch now, solves this as well. Will be in next release, hopefully tomorrow.

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

No branches or pull requests

4 participants