Program crashes due to stack overflow and access violation exceptions

1

The problem is that every time, when I try to link related tables of a many-to-many relationship, I get stack overflow and access violation exceptions.

Here is what i receive in debug console:

info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001

info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000

info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down.

info: Microsoft.Hosting.Lifetime[0]

Hosting environment: Development

info: Microsoft.Hosting.Lifetime[0] Content root path: C:\Users\vynar\source\repos\IndividualProjects\E_Library\API

Stack overflow.

C:\Users\vynar\source\repos\IndividualProjects\E_Library\API\bin\Debug\netcoreapp3.1\ELibrary.API.exe (process 14384) exited with code -1073741819.


in a VS output window(only a part of it):

Microsoft.Hosting.Lifetime: Information: Content root path: > C:\Users\vynar\source\repos\IndividualProjects\E_Library\API 'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Net.Security.dll'.

Exception thrown: 'System.IO.IOException' in System.Net.Se``curity.dll

Exception thrown: 'System.IO.IOException' in System.Net.Security.dll

Exception thrown: 'System.IO.IOException' in System.Net.Security.dll

Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Net.Http.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Users\vynar\source\repos\IndividualProjects\E_Library\API\bin\Debug\netcoreapp3.1\Microsoft.IdentityModel.Protocols.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Users\vynar\source\repos\IndividualProjects\E_Library\API\bin\Debug\netcoreapp3.1\Microsoft.IdentityModel.Protocols.OpenIdConnect.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Net.WebSockets.dll'.

Exception thrown: 'System.IO.IOException' in System.Net.Security.dll

Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Users\vynar\source\repos\IndividualProjects\E_Library\API\bin\Debug\netcoreapp3.1\Microsoft.IdentityModel.JsonWebTokens.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Runtime.Numerics.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.1\System.Runtime.Serialization.Primitives.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.1\Microsoft.AspNetCore.WebUtilities.dll'.

'ELibrary.API.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.1\Microsoft.AspNetCore.Http.Extensions.dll'.

The program '[14384] ELibrary.API.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Code that probably causes the crash:

Controller:

[Authorize(Roles = "Admin, Manager")]
    [HttpPost("attach")]
    public async Task<IActionResult> Attach([FromBody] AuthorBookModel authorBook)
    {
        var createdLink = await _service.Attach(authorBook.BookId, authorBook.AuthorId);
        if (createdLink == null)
        {
            return BadRequest(authorBook);
        }
        return CreatedAtAction(nameof(GetAllBooks), createdLink);
    }

Service

public async Task<AuthorBook> Attach(int bookId, int authorId)
    {
        var book = await _unitOfWork.Books.GetByIdAsync(bookId);
        var author = await _unitOfWork.Authors.GetByIdAsync(authorId);
        if (book == null && author == null)
        {
            return null;
        }
        var authorBook = new AuthorBook() { AuthorId = author.Id, BookId = book.Id };

        book.AuthorBooks.Add(authorBook);

        author.AuthorBooks.Add(authorBook);

        await _unitOfWork.CommitAsync();

        return authorBook;

    }

And it is also worth mentioning that code works, data is inserted

c#
.net
asp.net-core
asked on Stack Overflow Feb 9, 2020 by Nazar Vynar • edited Feb 9, 2020 by Nazar Vynar

1 Answer

0
var authorBook = new AuthorBook() { AuthorId = author.Id, BookId = book.Id };

book.AuthorBooks.Add(authorBook);

author.AuthorBooks.Add(authorBook);

This is a code smell. Why do you want to trigger saving new AuthorBook on each navigation property inside 2 different tables?

In those tables, all you need to do is store AuthorBookId, even then, there is no need to do it. In AuthorBooks table, store only BookId and AuthorId. That way you are giving AuthorBooks table the responsibility to manage relations instead of doing that in every table.

Try changing what I said and tell if there is any bug then.

answered on Stack Overflow Feb 9, 2020 by Ryukote

User contributions licensed under CC BY-SA 3.0