ericsink / SQLitePCL.raw

A Portable Class Library (PCL) for low-level (raw) access to SQLite
Apache License 2.0
512 stars 106 forks source link

"Attempted to read or write protected memory" exception in v2.1.0 #498

Closed Gusi closed 1 year ago

Gusi commented 2 years ago

The following code was working fine in previous releases (last tested v2.0.7) but in v2.1.0 is throwing: $exception {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} System.AccessViolationException HResult -2147467261 int

public static IList<T> SqlQuery<T>(this DbContext db, string sql, params object[] parameters) where T : class
{
    using (var db2 = new ContextForQueryType<T>(db.Database.GetDbConnection()))
    {
        var ret = db2.Set<T>().FromSqlRaw(sql, parameters);
        return ret.ToList(); // EXCEPTION HERE
    }
}

private class ContextForQueryType<T> : DbContext where T : class
{
    private readonly DbConnection connection;

    public ContextForQueryType(DbConnection connection)
    {
        this.connection = connection;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(connection);// options => options.);
        base.OnConfiguring(optionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<T>().HasNoKey();
        base.OnModelCreating(modelBuilder);
    }
}

Exception stack trace:

Fatal error. 0xC0000005
   at SQLitePCL.SQLite3Provider_sqlite3+NativeMethods.sqlite3_step(SQLitePCL.sqlite3_stmt)
   at SQLitePCL.SQLite3Provider_sqlite3.SQLitePCL.ISQLite3Provider.sqlite3_step(SQLitePCL.sqlite3_stmt)
   at SQLitePCL.raw.sqlite3_step(SQLitePCL.sqlite3_stmt)
   at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(System.Data.CommandBehavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(System.Data.CommandBehavior)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1+Enumerator[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].InitializeReader(Enumerator<System.__Canon>)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1+Enumerator+<>c[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<MoveNext>b__19_0(Microsoft.EntityFrameworkCore.DbContext, Enumerator<System.__Canon>)
   at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.__Canon,Boolean>, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.__Canon,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Boolean>>)
   at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1+Enumerator[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext()
   at System.Collections.Generic.List`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)
   at System.Linq.Enumerable.ToList[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.IEnumerable`1<System.__Canon>)

I'm not sure if this will be enough to catch the bug, so please ask me for more details if needed. Currently using packages SQLitePCLRaw.core and SQLitePCLRaw.provider.sqlite3.

ericsink commented 2 years ago

Yeah I'll probably need more info. Can you post a minimal repro project?

Gusi commented 1 year ago

Fixed in v2.1.1 Thanks!