System.AccessViolationException: 尝试读取或写入受保护的内存

依旧是开发Android平板客户端与C#pc端通讯时遇到的问题。

问题描述:

当平板连接电脑时,已经有其他便携设备插在电脑上,此时用户登录pc端,pc只能部分用户登录,其他用户会使得程序坏死,弹出的错误是:

c#开发中遇到System.AccessViolationException

[Managed to Native Transition]
mscorlib.dll!System.RuntimeTypeHandle.GetTypeByName(string name, bool throwOnError, bool ignoreCase, bool reflectionOnly, ref System.Threading.StackCrawlMark stackMark, System.IntPtr pPrivHostBinder, bool loadTypeFromPartialName)
mscorlib.dll!System.Type.GetType(string typeName)
System.Data.dll!System.Data.Common.DbProviderFactories.GetFactory(System.Data.DataRow providerRow)
System.Data.Entity.dll!System.Data.EntityClient.EntityConnection.GetFactory(string providerString)
System.Data.Entity.dll!System.Data.EntityClient.EntityConnection.ChangeConnectionString(string newConnectionString)
System.Data.Entity.dll!System.Data.Objects.ObjectContext.CreateEntityConnection(string connectionString)
TAXSystem.Entity.dll!TAXSystem.Entity.TaxSystemEntities.TaxSystemEntities()
TaxSystem.exe!TaxSystem.UIFrm.JH_MagFrm.getWaitryNum()
TaxSystem.exe!TaxSystem.UIFrm.JH_MagFrm.JH_MagFrm(string pID)
TaxSystem.exe!TaxSystem.Program.Main().

程序停止运行的地方在:


 TaxSystemEntities taxEntity = new TaxSystemEntities()这报错。

经过查看,此处的实例化不存在问题,但是找不到任何其他相关的错误。并且,拔掉U盘后,PC程序又能使用异常的账号正常登陆了。还有一种情况就是,在vs中调试运行时是不存在这个错误的。只有当程序拷贝到Windows 环境中才出现这样的错误。

各方查询无果,折腾两天也没有解决方案。直到最后才看到:http://q.cnblogs.com/q/59907/。 发现,这种异常还与系统本身的环境有关,

netsh winsock reset   --运行此命令,解决。 ----坑死人啦~~~

百度:

netsh winsock reset命令,作用是重置 Winsock 目录。如果一台机器上的Winsock协议配置有问题的话将会导致网络连接等问题,就需要用netsh winsock reset命令来重置Winsock目录借以恢复网络。这个命令可以重新初始化网络环境,以解决由于软件冲突、病毒原因造成的参数错误问题。 netsh是一个能够通过命令行操作几乎所有网络相关设置的接口,比如设置IP,DNS,网卡,无线网络等,Winsock是系统内部目录,Winsock是Windows网络编程接口,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口,reset是对Winsock的重置操作。当执行完winsock的命令重启计算机后,需要重新配置IP。


 在有vs2010后 再安装vs2013 重启主机  访问部署iis上的vs2010项目 就出现以上错误 

  系统日志中也出现

错误应用程序名称: w3wp.exe,版本: 7.5.7601.17514,时间戳: 0x4ce7afa2
错误模块名称: ntdll.dll,版本: 6.1.7601.18229,时间戳: 0x51fb164a
异常代码: 0xc0000005
错误偏移量: 0x000000000004e4e4
错误进程 ID: 0xbe0
错误应用程序启动时间: 0x01d0baaf598cfaf3
错误应用程序路径: c:\windows\system32\inetsrv\w3wp.exe
错误模块路径: C:\Windows\SYSTEM32\ntdll.dll
报告 ID: 97a8ac9c-26a2-11e5-a5a4-f46d043db86。



下边保留解决方案原文:


:windows 7 64位,vs2010.

windows 程序,在创建项目的时候,项目默认编译目标平台为X86。

功能:只连接数据库,本机。

第一个字符串为127.0.0.1;

第二个字符串为:.;

 string connectionstring1 = "Data Source=127.0.0.1;Initial Catalog=YRB;User ID=sa;Password=sa;Persist Security Info=True";

            string connectionstring2 = "Data Source=.;Initial Catalog=YRB;User ID=sa;Password=sa;Persist Security Info=True";
  
            using (SqlConnection conn = new SqlConnection(connectionstring1))
            {
                conn.Open();
                conn.Close();
            }

            using (SqlConnection conn = new SqlConnection(connectionstring2))
            {
                conn.Open();
                conn.Close();
            }

问题出来了:

第一个连接字符串在打开连接时报异常,如下:

未处理 System.AccessViolationException
  HResult=-2147467261
  Message=尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
  Source=System.Data
  StackTrace:
       在 SNINativeMethodWrapper.SNIClose(IntPtr pConn)
       在 System.Data.SqlClient.SNIHandle.ReleaseHandle()
       在 System.Runtime.InteropServices.SafeHandle.InternalDispose()
       在 System.Runtime.InteropServices.SafeHandle.Dispose(Boolean disposing)
       在 System.Runtime.InteropServices.SafeHandle.Dispose()
       在 System.Data.SqlClient.TdsParserStateObject.Dispose()
       在 System.Data.SqlClient.TdsParser.Disconnect()
       在 System.Data.SqlClient.SqlInternalConnectionTds.LoginFailure()
       在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
       在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
       在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       在 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       在 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       在 System.Data.SqlClient.SqlConnection.Open()
       在 ConsoleApplication1.Program.Main(String[] args) 位置 C:\Users\WANGJD\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:行号 25
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

 

第二个正常。

经测试,在连接字符串中无论使用IP地址或域名做为数据库服务器目标时,都会出现以上异常。

----------------------------

但当将项目的目标平台设置为AnyCPU时,所有情况都正常,求解!!!!

补充:上周2月26号,电脑自动更新了好多关于.Net 的部分(包括Vs2010),从此电脑中所有运行正常的项目(连接数据库部分)都出现以上异常,无法运行!!!








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值