Search code examples
c#.net.net-coreblazorblazor-webassembly

How to fix Blazor WASM memory access out of bounds error?


Started a new Blazor WebAssembly project (Windows 10, Visual Studio 2019), trying to get a simple list via HttpClient from the server side.

Error:

00755c3a:0xa3a0 Uncaught (in promise) RuntimeError: memory access out of bounds
    at malloc (<anonymous>:wasm-function[359]:0xa3a0)
    at monoeg_malloc (<anonymous>:wasm-function[161]:0x3c71)
    at stack_frag_new (<anonymous>:wasm-function[2019]:0x59758)
    at add_frag (<anonymous>:wasm-function[1430]:0x3ccf2)
    at interp_exec_method (<anonymous>:wasm-function[1120]:0x2f609)
    at interp_runtime_invoke (<anonymous>:wasm-function[5655]:0xf7391)
    at mono_jit_runtime_invoke (<anonymous>:wasm-function[5109]:0xddb3d)
    at do_runtime_invoke (<anonymous>:wasm-function[1410]:0x3ba85)
    at mono_runtime_try_invoke (<anonymous>:wasm-function[418]:0xcfdb)
at mono_runtime_invoke (<anonymous>:wasm-function[1610]:0x44b39)

Server side (this returns properly):

    [HttpGet]
    public async Task<IEnumerable<UserDetailsRM>> Get()
    {
        var users = await UserService.GetUsers();
        return users;
    }

client side: doesn't render anything despite getting results back.

<div class="userListWrapper">
        @if (Users != null)
        {
            <table class="table table-borderless" style="font-size:12px; font-family:arial; height: 
                500px; display:block;   overflow-y:scroll">
                <thead>
                    <tr>
                        <th scope="col">Profile</th>
                        <th scope="col">Full Name</th>
                        <th scope="col">Gender</th>
                        <th scope="col">Age</th>

                    </tr>
                </thead>

                @foreach (var user in Users)
                {
                    <tbody class="scrollableTable" style="border-radius:15px;">
                        <tr class="userRow" >
                            <td><img src="@user.ProfileImageUrl" style="width:30px;border- 
                             radius:30px" /></td>
                            <td>@user.FullName</td>
                            <td>@user.Gender</td>
                            <td>@user.Age</td>
                        </tr>

                    </tbody>
                }
            </table>
        }
  </div>

@code{

private IEnumerable<UserDetailsRM> Users;
string LoggedEmail;
string UserId;
string UserActionMessage = "No Logged User";
string Wanted = "";
protected async override Task OnInitializedAsync()
{
    Users = await Http.GetFromJsonAsync<IEnumerable<UserDetailsRM>>("user");
}
}

client project file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="5.0.0-preview.6.20312.15" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-rc.2.20475.17" PrivateAssets="all" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Domain\Domain.csproj" />
  </ItemGroup>
  
</Project>

server project file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.1" />
    <PackageReference Include="NetTopologySuite" Version="2.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\DataAccess\DataAccess.csproj" />
    <ProjectReference Include="..\Client\TeamAppManager.Client.csproj" />
  </ItemGroup>


</Project>

Would appreciate any help. Thanks.

Edit: Models:

public class UserDetailsRM :  IIntEntity
{
  
    public int Id { get; set; }
    public int UserDetailsId { get; set; }
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthdate { get; set; }
    public int UserGenderOptionId { get; set; }
    private string gender;
    public string Gender
    {
        get { return gender; }
        set
        {
            if (value == "1")
                gender = "Male";

            else if (value == "2")
                gender = "Female";

            else
                gender = "Other";
        }
    }
    public string FullName
    {
        get 
        {
            return $"{FirstName} {LastName}";
        }
        set
        {
            FullName = value;
        }
    }

    public int Age
    {
        get
        {
            var today = DateTime.Today;
            var age = today.Year - Birthdate.Year;
            if (Birthdate.Date > today.AddYears(-age)) age--;
            return age;
        }
    }

    public string ProfileImageUrl { get; set; }

    
}

Solution

  • I guess the issue is with this property which causes the error

    public string FullName
        {
            get 
            {
                return $"{FirstName} {LastName}";
            }
            set
            {
                FullName = value;
            }
        }
    

    Remove the set accessor and run your code.