AccessViolationException in OdbcDataReader.Dispose ()
I am getting this exception when disposing of the OdbcDataReader right after fetching data from the database. In fact, the reader is left in the "Usage" block. As far as I know, this shouldn't throw any errors. Any ideas?
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Data.Common.UnsafeNativeMethods.SQLFreeStmt(OdbcStatementHandle StatementHandle, STMT Option)
at System.Data.Odbc.CMDWrapper.FreeStatementHandle(STMT stmt)
at System.Data.Odbc.OdbcDataReader.Close(Boolean disposing)
at System.Data.Odbc.OdbcDataReader.Dispose(Boolean disposing)
at System.Data.Common.DbDataReader.Dispose()
at MyNamespace.MyClass.MyFunction() in C:\myfile.vb:line 100
Thank!
EDIT: Using Sybase ASE 12.5 database
source to share
Is an exception thrown in Dispose()
? Or earlier? Bad practice for Dispose()
throwing, but not unheard of. Sometimes (for example, with WCF ) it is desirable to swallow them, but in this case the error sounds like something that cannot be internalized.
A quick search OdbcDataReader
+AccessViolationException
reveals that this is not uncommon - but there is not enough information (database? ") In the question to narrow it down. Personally, I would start by looking at some google hits ... (maybe filtered by your rdbms).
source to share
In general, managed / no-change interactions can be frustrating when it comes to lifecycle management.
It looks like a race condition between dispose and a finalizer inside the OdbcDataReader, which is probably deleting some kind of unmanaged object that is also trying to clean up. They are probably both called SQLFreeStmt.
Using guarantees to clean up an object up - but that doesn't guarantee that the object will be preserved. [The finalizer may strike during the last call to the object instance method. Therefore, the finalizer can strike during the call. If it happens early enough, itβs as if the finalizer is called before the disposal.]
If that's true, and if the OBDC object is a black box for you, it's best to try and do a workaround.
I would try to add System.GC.KeepAlive (your_DbDataReader_object) on the last line to the end of your scope.
For a full overview of the headaches you throw and finalizers see: GC Discussion This is a very detailed discussion.
source to share