Results 1 to 5 of 5

Thread: [RESOLVED] VB 2010 protected memory exception in command form.close

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    3

    Resolved [RESOLVED] VB 2010 protected memory exception in command form.close

    Hi!
    I'm having a problem with a very old VB program I developed many years ago.
    "system access violation exception attempted to read or write protected memory"

    The exception appears when I try to close one particular form.
    I'm using .Net 4.0
    My system is Windows 10 x64

    What additional information do you need me to add?

    Thank you all in advance!
    Last edited by Josete; May 28th, 2024 at 04:50 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,535

    Re: VB 2010 protected memory exception in command form.close

    What that form does and how it does it would be a start, especially any code that might be executed on close. If this happens in the debugger, I would create a new form and configure it the same way and see whether it behaves the same way in place of the old form.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    3

    Re: VB 2010 protected memory exception in command form.close

    There are many forms.
    The main form is called frmTotApp. It's used as container for many forms sons (MdiChildren).
    The second form is called fromTotMnu and is used as a menu.

    In the frmTotMnu you can go to all the different sections of the aplication with a menuStrip or with big buttons.
    You can exit the aplication:
    1) using the menu, clicking Exit or with Alt+F4 (mnuFleEnd)
    2) clicking the Exit button (btnEnd)
    3) clicking the close window button
    The code that controls the first 2 options is ...
    Code:
    Private Sub spEnd(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFleEnd.Click, btnEnd.Click
        Try
            Me.Close()
        Catch ex As Exception
            'Don't do anything
        End Try
    End Sub
    Option 1 and 2 generate an exception in the command Me.Close

    If I click in the Close Window button ... the same exception ocurrs in the following code of frmTotApp.
    Code:
    Private Sub Form_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim vlSpl As frmModSpl 'Splash form
        If clsGblSta.Dir = clsAbsEnm.Dir.Fin Then
           For Each vlFrm As Form In MdiChildren
               vlFrm.Close() 'Here is where the exception ocurrs
           Next
           Me.Hide()
           If clsGblUsu.BlnOpd Then
               vlSpl = New frmModSpl 'Instance of Splash
               vlSpl.Main(clsAbsEnm.Getting.UsuEnd) 'Close user
               vlSpl.Dispose() 'Cleaning
               vlSpl = Nothing 'Cleaning
           End If
        Else
           e.Cancel = True
        End If
    End Sub
    "AccessViolationException was unhandled" Attempted to read or write protected memory ...

    Any ideas?
    Thanks in advance!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: VB 2010 protected memory exception in command form.close

    I haven't seen anything like this, so I'm just speculating, but you couldn't remove items from a collection like that.

    The first thing I would try would be simple and likely quite useless: Check to make sure that MdiChildren has a count > 0. I don't think this could be false, because if it were, then the loop would never run and you wouldn't get an exception, but I'd do it just to see what that number was.

    The second thing I would try would be a For loop rather than a For Each, stepping in reverse order. This would be just because you couldn't remove items from a list in this fashion. Forms are a bit weird in exactly how they act and how Close works for them. This may not solve the problem, but it might shed some light on it.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2024
    Posts
    3

    Re: VB 2010 protected memory exception in command form.close

    Well, you gave me all the clues I needed.

    The order of the forms that I was trying to close was very important.
    When trying to close them one by one without considering the order, the process became recursive.
    Thus the program tried to close forms that were closing.

    I was making my life unnecessarily complicated.
    I just didn't need to close all the mdichildren.

    Now I simply rely on the main form to close its children.
    I have simply deleted the "For...Each" code and everything works fine.

    Thanks a lot!!
    Thanks again!!
    More thanks!!
    Many more thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width