简体   繁体   中英

Exception: Attempted to read or write protected memory

My Project Contain call to SetSession ManyTime SomeTime it's Crashing. Please any one can help me.

Here section code:

public Function SetSession(sSessionName As String) As Boolean
Dim intCount As Integer
'Call Refresh
SetSession = False
Dim oSessions As ExtaSessions = Nothing
Dim oSession As ExtraSession = Nothing
moSession = Nothing
oSessions=TryCast(moSystem.Sessions,ExtraSessions)
moSession=TryCast(oSessions.Item(sSessionName),ExtraSession)'=> Crashing in this line
If Information.Err().Number <> 0 Then
For intcount = 1 To  oSessions.Count
oSession = TryCast(oSessions.Item(intCount), ExtraSession)
if oSession.Name = sSessionName then
moSession = oSession
Exit for
End if
NExt
End if
if moSession is Nothing then
Exit Function End If
SetSession = True
End Function

In following attachment PrintScreen for Exception

在此处输入图片说明

Finnaly I fix my problem

before change:

Dim oSessions As ExtaSessions = Nothing
Dim oSession As ExtraSession = Nothing
moSession = Nothing
oSessions=TryCast(moSystem.Sessions,ExtraSessions)
moSession=TryCast(oSessions.Item(sSessionName),ExtraSession) 

after change:

Dim oSessions As EXTRA.ExtraSessions
Dim oSession As EXTRA.ExtraSession
moSession = Nothing
moSystem = New Extra.ExtraSystem
Try
oSessions = DirectCast(moSystem.Sessions,EXTRA.ExtraSessions)
moSession = DirectCast(oSessions.Item(sSessionName),EXTRA.ExtraSession)
Catch ex As Exception
Interaction.MsgBox(ex)
End Try

I just Init moSystem to Extra.ExtraSystem and i changed ExtaSessions to EXTRA.ExtraSessions and DirectCast to trycast and it's work 100%

thx :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM