CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    4

    VB IDE Crashes When Unloading Form

    I am currently using VB6 with SP6 and whenever I use the UNLOAD FORMNAME within the module or even use the following code in a function

    Code:
    Public Function CloseAllForms()
        ' Unload and set each form to Nothing (remove it from memory)
        Dim OpenForm As Form
    
        For Each OpenForm In Forms
            Unload OpenForm
            Set OpenForm = Nothing
        Next OpenForm
    
    End Function
    the IDE crashes. Though when I use this particular function that exists in another program it works ok and I copied the code from my code library in both cases (copy-paste). In one case it accesses access 2000 database and in another it accesses a text file.

    I am not sure where to start looking since this code works in some programs but not others and I copied and pasted the code from my code library in ALL cases.

    Thanks for any and all help that anyone can offer or suggest.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: VB IDE Crashes When Unloading Form

    Well one change that most of our senior members will agree on, is
    Code:
    Public Function CloseAllForms()
    ' Unload and set each form to Nothing (remove it from memory)
      Dim OpenForm As Form
     
      For Each OpenForm In Forms
    	If OpenForm.HDC <> Me.HDC Then
    	  Unload OpenForm
    	  Set OpenForm = Nothing
    	End if
      Next OpenForm
      Unload Me
    End Function
    Gremmy...
    Last edited by GremlinSA; May 30th, 2007 at 11:31 AM.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: VB IDE Crashes When Unloading Form

    Quote Originally Posted by DaWolf
    I am not sure where to start looking since this code works in some programs but not others and I copied and pasted the code from my code library in ALL cases.

    Thanks for any and all help that anyone can offer or suggest.
    First step would be to add the code as Gremmy shows so you don't call Unload on the form in the middle of your function. Next is to debug each form unloading.

    Put a breakpoint on the line with the Unload call and run your application and quit like you normally do when it crashes. It should hit the breakpoint and you should be able to step with F8 and enter the Unload event of the form it told to unload.

    If you step through each Unload event and your IDE suddenly crashes, you should have a good idea on what could be causing it, or at least which Form is causing a problem.

    Oh, and the line Set OpenForm = Nothing in the For loop isn't needed.
    Last edited by ChaosTheEternal; May 30th, 2007 at 01:56 PM.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: VB IDE Crashes When Unloading Form

    Oop's .. i didn't notice that, I just copy pased the OP's code and did a quick edit.. But your right about Set Form = nothing not been needed...

    The rest of your post is spot on, very good debuging advice...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: VB IDE Crashes When Unloading Form

    Well, I just noticed that it's a Public Function, so maybe it's located in a module and not a form, in which case there is no Me object so the check that you're not unloading wouldn't work.

    Unless you passed the form object ByRef to it:
    Code:
    Public Function CloseAllForms(ByRef CallingForm As Form)
    ' Unload and set each form to Nothing (remove it from memory)
        Dim OpenForm As Form
     
        For Each OpenForm In Forms
    	If OpenForm.HDC <> CallingForm.HDC Then Unload OpenForm
        Next OpenForm
        Unload CallingForm
    End Function
    And I just got curious. Why check the hDC? I would generally check or compare by the hWnd.

    Not that it doesn't work with hDC, but I'm just curious.
    Last edited by ChaosTheEternal; June 1st, 2007 at 10:56 AM. Reason: Ignored my own comment by copy/pasting.

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: VB IDE Crashes When Unloading Form

    Quote Originally Posted by ChaosTheEternal
    And I just got curious. Why check the hDC? I would generally check or compare by the hWnd.

    Not that it doesn't work with hDC, but I'm just curious.
    Well,i remeberd it was "H" Something, and HDC was first on intelisence. And i think someone also posted this code with hDC before.....

    But like you said .. it will work..

    BTW - I see you also Copy Pasted !!!
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Apr 2007
    Posts
    4

    Resolved Re: VB IDE Crashes When Unloading Form

    Thanks to all for your help. I changed the code as GremlinSA suggested and I no longer had the IDE crashing. I am not sure what the original cause was but it appears to be resolved at this time.

    Again thanks to everyone

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

Featured