Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempted to read or write protected memory-Sql Compact and .NEt

I've tried to run the application in two situations: with SQL Server compact edition (3.5) installed and one without. When SQL Server Compact edition isn't installed, I get the following error:

Exception :Attempted to read or write protected memory. This is often an indication
that other memory is corrupt.

Inner Exception : 
========================

Stack Trace :
========================
   at System.Data.SqlServerCe.NativeMethods.CloseStore(IntPtr pSeStore)
   at System.Data.SqlServerCe.SqlCeConnection.ReleaseNativeInterfaces()
   at System.Data.SqlServerCe.SqlCeConnection.Dispose(Boolean disposing)
   at System.Data.SqlServerCe.SqlCeConnection.Finalize()
Source : 
========================
System.Data.SqlServerCe
----------------------------------------------------------

I'm handling Unhandled exceptions using this method.

I am getting this error from a console application which I'm starting from a Windows Form. In both the application I've inserted the Unhandled Exception coding and it's getting executed and getting returned to the text file, and the Microsoft 'report error' dialog is getting generated; how do I keep that from happening? How do I troubleshoot what is causing this issue?

like image 981
Jankhana Avatar asked Nov 15 '22 12:11

Jankhana


2 Answers

When I had this issue, the problem was the database connection:

If you don't close the connection before reopen it, you get this error arround "System.Data.SqlServerCe".

Just adding this line solved my issue:

SqlCeConnection conn = new SqlCeConnection(_connectionString);
conn.Close(); //adding this.


if(conn == ConnectionState.Open) {
//-- Your code here.
}
like image 182
pablocervio Avatar answered Dec 21 '22 02:12

pablocervio


The native SQL Compact code is crashing. That's very unusual. And very unlikely to be caused by your code. Not the kind of problem you could ever debug, you don't have anything but machine code to look at. If you run any other in-process unmanaged code then consider it to be responsible for the instability. Writing off the PC and shooting it between the disk platters is the quick fix.

like image 29
Hans Passant Avatar answered Dec 21 '22 02:12

Hans Passant