ngx-bootstrapngx-bootstrap-modal

Modal memory leak as more modals added


I am developing an Angular 4 application using ngx-bootstrap modals heavily. I am currently using the template + modalService way of opening up modals. During a click event, this line of code is called:

@ViewChild() worklistTemplate;
// ...
this.modalRef = this.modalService.show(this.worklistTemplate, this.config);

And the worklist template looks like this:

<ng-template #worklistTemplate>
  <app-action-view
    [leftButtons]="leftButtons"
    [labelHeader]="modalHeader"
    [labelIcon]="modalIcon"
    [modalRef]="modalRef">
    <div class="row modal-info-panel">
      <div class="col-xs-4 modal-user-info">
        <fa name="mars" class="gender-icon"></fa>
        <div class="field-panel">
          <input type="text"
            [ngModel]="rowInfo.lastName"
            [ngClass]="{ 'modal-editable-field': modalFieldsEditable }"
            [disabled]="!modalFieldsEditable">
          <input type="text"
            [ngModel]="rowInfo.firstName"
            [ngClass]="{ 'modal-editable-field': modalFieldsEditable }"
            [disabled]="!modalFieldsEditable">
          <div>
            <label for="modal-mrn">MRN --</label>
            <input id="modal-mrn" type="text"
              [ngModel]="rowInfo.mrn"
              [ngClass]="{ 'modal-editable-field': modalFieldsEditable }"
              [disabled]="!modalFieldsEditable">
          </div>
          <div>
            <label for="modal-dob">DOB --</label>
            <input id="modal-dob" type="text"
              [ngModel]="rowInfo.birthDate"
              [ngClass]="{ 'modal-editable-field': modalFieldsEditable }"
              [disabled]="!modalFieldsEditable">
          </div>
          <div class="edit-panel">
            <fa *ngIf=modalFieldsEditable class="worklist-edit-buttons green-hover link" name="floppy-o" tooltip="Save" (click)=saveChanges()></fa>
            <fa *ngIf=modalFieldsEditable class="worklist-edit-buttons red-hover link" name="times" tooltip="Cancel" (click)=toggleModalFields()></fa>
          </div>
        </div>
      </div>
      <div class="col-xs-8 modal-id-info">
        Associated ID
        <div class="full-width">
          <div class="row" *ngFor="let i of rowInfo.associatedIDs">
            <div class="col-xs-6 cell">{{i.id}}</div><div class="col-xs-6 cell">{{i.source}}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="row" id="modal-table">
      <app-table-view
            [columns]="objDetailsCols"
            [tableData]="objDetailsData"
            [expandTemplate]="rowImageContainer"
            [expandColStyle]="expandColStyle">
      </app-table-view>
    </div>
    <div *ngIf="resolvePanelVisible" class="resolve-panel"
      [@slideRight]>
      <div class="resolve-label">
        <fa class="lt-icon" name="wrench"></fa>
        Resolve QA Issue
      </div>
      <div class="resolve-body">
        Hello, World!
      </div>
      <div class="resolve-footer">
        <a>Validate</a>
        <a>Resolve</a>
        <a>Reload</a>
        <a (click)="toggleResolvePanel()">Close</a>
      </div>
    </div>
  </app-action-view>
</ng-template>

The modal can be closed by clicking outside of the modal boundaries or there is a button inside the ActionViewComponent that calls modalRef.hide().

Memory usage increases drastically as more and more modals are opened. In fact, if I open the modal around 5 times, the application becomes sluggish and almost unusable. This is especially apparent if there are many rows in our TableViewComponent.

Is there a problem with the way I am using the modals, or is this an issue related to ngx-bootstrap modals? OR, is the issue related to the browser and unavoidable? I am developing on Chrome 62 right now.

I have verified that onDestroy is never called on the TableViewComponent inside the modal. It is not even called if I navigate to a different page component, though another table (not in the template) does have onDestroy called when navigating from the page.

Any feedback is appreciated greatly- I have been stuck for way too long on this.


Solution

  • This is an issue of ngx-bootstrap, unfortunately. I created a pull request (https://github.com/valor-software/ngx-bootstrap/pull/3179) so this will be fixed as soon as my PR is merged and new version is released.