RadzenTextBox memory leak?

When you declare RadzenTextBox with two way binding on property's object member (SelectedAcceptance.Number) as follow:

@inherits AcceptanceViewModel

@using Radzen

<RadzenTemplateForm TItem="IAcceptanceData"
					Data="@this">
	<FluentValidationValidator @ref="Validator" />	
			<RadzenTextBox @bind-Value=@SelectedAcceptance.Number />		

           <BarcodeComponent 
                                Scanned="OnBarcodeScanned"
                                Changed="OnBarcodeChanged"
                                Disabled="@IsLoading" />		
<RadzenTemplateForm>

Then this part of code will subscribe each time when parameters set

public class FormComponent<T> : RadzenComponent, IRadzenFormComponent
    {
        ....
 public override Task SetParametersAsync(ParameterView parameters)
        {
            var result = base.SetParametersAsync(parameters);

            if (EditContext != null && ValueExpression != null && FieldIdentifier.Model != EditContext.Model)
            {
                FieldIdentifier = FieldIdentifier.Create(ValueExpression);
                EditContext.OnValidationStateChanged += ValidationStateChanged;
            }

            return result;
        }

Look at if block:

  1. EditContext is not null (I use RadzenTemplateForm with FluentValidationValidator)
  2. ValueExpression is not null RadzenTextBox is two-way binded
  3. The problem is here
    FieldIdentifier.Model - this is reference to type of property SelectedAcceptance
    EditContext.Model - this is reference to type of 'this' AcceptanceViewModel class contains property SelectedAcceptance which type contains property Name which two-way binded on RadzenTextBox.
    So 'if' condition is true each time and each time when method SetParametersAsync is invoked (this happens each time when user enters data to BarcodeComponent due to blazor components lifecycle) EditContext.OnValidationStateChanged is subscribed again and again to handler ValidationStateChanged. After user enters data 100-150 times (100-150 scanned barcodes) it's significantly slow down app due to invoking StateHasChanged inside handler ValidationStateChanged 100-150 times on each data enetering
 private void ValidationStateChanged(object sender, ValidationStateChangedEventArgs e)
        {
            StateHasChanged();
        }

this could be fixed just do not bind to Property's member, but I think this is not obvious and just workaround

Hi @Pavlo,

We will update the code to look like this:

            if (EditContext != null && ValueExpression != null && FieldIdentifier.Model != EditContext.Model)
            {
                FieldIdentifier = FieldIdentifier.Create(ValueExpression);
                EditContext.OnValidationStateChanged -= ValidationStateChanged;
                EditContext.OnValidationStateChanged += ValidationStateChanged;
            }

This should unsubscribe before subscribing and should avoid any leaks.

Hi @korchev ,

Thank you for the quick reply! This will help, but I thought the solution is to change 'if' condition to prevent fall into if block. But it's only my assumption. Anyway thank you a lot.