Results 1 to 17 of 17

Thread: [RESOLVED] How to end my program properly?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Resolved [RESOLVED] How to end my program properly?

    Hello Vbforums
    When ending my program, widows sends this message error but vb6 doesn't send any error.
    Name:  pic.png
Views: 506
Size:  12.9 KB
    In my form unload event I'm destroying all forms and disabling all timers.
    In the IDE, when I lauch the application and thei I exit I don't get this message from windows.

    When I drop the EndProgram code from the unload event, there is no message error from windows but the program is still running in the Task Manager.

    Please any idea?
    thank you all

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,625

    Re: How to end my program properly?

    It sounds like an object created somewhere along the way isn't getting freed automatically, or perhaps something in the end program routine is referencing something that causes a new instance?

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: How to end my program properly?

    Do you have an Errorhandler in your Unload-Event?
    Setting a Breakpoint there?
    That way you can check what's causing your error.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Thanks fafalone
    Even when I used
    Code:
    End
    in Form unload event, the error is still there.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Quote Originally Posted by Zvoni View Post
    Do you have an Errorhandler in your Unload-Event?
    Setting a Breakpoint there?
    That way you can check what's causing your error.
    What is causing the error is this code:
    Code:
    Sub EndProgram()
    Dim tmpForm As Form
    For Each tmpForm In Forms
    If tmpForm.Name <> "MaiForm" Then
    Unload tmpForm
    Set tmpForm = Nothing
    End If
    Next
    End Sub

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,382

    Re: How to end my program properly?

    Is that a Typo? "MaiForm"?
    Sure it's not "MainForm"?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Quote Originally Posted by Zvoni View Post
    Do you have an Errorhandler in your Unload-Event?
    Setting a Breakpoint there?
    That way you can check what's causing your error.
    What is causing the error is this code:
    Code:
    Sub EndProgram()
    Dim tmpForm As Form
    For Each tmpForm In Forms
    If tmpForm.Name <> "MaiForm" Then
    Unload tmpForm
    Set tmpForm = Nothing
    End If
    Next
    End Sub

  8. #8
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: How to end my program properly?

    are you using a manifest in the app?

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: How to end my program properly?

    Code:
    
    Sub EndProgram()
        Dim tmpForm As Form
        For Each tmpForm In Forms
            If tmpForm.Name <> "MaiForm" Then
                Unload tmpForm
                Set tmpForm = Nothing
            End If
        Next
    End Sub
    
    

    Aside from the possible misspelling of "MaiForm", I've got a couple of comments about that code.

    First, the "Set tmpForm = Nothing" line really isn't doing much of anything. Sure, it's clearing the reference, but that's going to happen when the For/Next loop loops anyway. Mustaphi, you may think it's uninstantiating the com/code object of the loaded form, but it's not. The only way to clear that com/code object is to clear ALL the references to it (of which tmpForm is just a temporary new one you've created). However, instantiated com/code objects are NOT going to stop a VB6 program from shutting down (so long as no code is currently being executed within them).

    Second, I don't see why you need the "If tmpForm.Name <> "MaiForm" Then" line. Is there some reason you want to unload this form last (or not at all)? In many cases, the order in which you unload forms won't matter. And don't mistakenly think that unloading the form is also uninstantiating the form's com/code object, because it's not. Also, if you're worried about uninstantiating code while it's running, that can't happen. When you call a procedure in a com/code object, it always creates an internal/temporary reference. This guarantees that any executing code has a chance to finish before the com/code object is uninstantiated. So, it seems that your loop could be simplified to the following, with no harm:

    Code:
    
    Sub EndProgram()
        Dim tmpForm As Form
        For Each tmpForm In Forms
            Unload tmpForm
        Next
    End Sub
    
    

    And lastly, if you're sure this EndProgram procedure is being executed, then it seems to me that you've got something in one of your forms that's keeping it from unloaded. This is quite easily done in the form_unload and/or form_queryunload events. Also, if you've got a combination of modal and non-modal forms loaded, you may get away with unloading them in a loop like this, but you may also have problems. I'll admit that I've got a loop similar to yours in my "main menu" form. However, I also work VERY hard to make sure that my program "backs out of" things in an organized and structured way. In fact, I have a check that reports to me whenever I've returned to my "main menu" and some straggler form is still open. That's a signal that I need to fix something. But that also depends on the structure and needs of your program. Form me, my "main menu" is hidden anytime I'm deeper into the program.

    Maybe some of that will help,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Quote Originally Posted by DEXWERX View Post
    are you using a manifest in the app?
    yes.

    But I dropped the manifest but the problem is still there.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Elroy
    Thanks a lot
    I did everything but no success.
    I deleted all forms and I kept only the MainForm but The windows error is still there when exiting the application.
    I deleted the manifest and the User Conntrols but without success.
    Thank you

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to end my program properly?

    Did you see post #6 seems like that may well be your issue.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Quote Originally Posted by DataMiser View Post
    Did you see post #6 seems like that may well be your issue.
    yes,
    that's just a mistake of mine

  14. #14
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,401

    Re: How to end my program properly?

    Some third-party libraries require special "tear down" calls before they are unloaded. If you happen to be using vbRichClient5 for example, it is recommended to call New_C.CleanupRichClientDll right before you leave the program. Other libraries I've used require you to make sure to specifically call cleanup methods if you've used certain objects, so make sure you have carefully read the documentation and are performing all such calls if you are using any third-party libraries.

    Another possibility is using a poorly declared API (for example, parameters declared with ByRef instead of ByVal or vice versa). Sometimes things will work while the process is running but as soon as it stops the walls cave in. Double-check all of your API declares if you are using any.

    Finally, circular references can be a problem. If you are referencing a library (A) that references another library (B), and that library also references the first library then you can run into trouble. If this is the case, then you might want to look into creating "weak" references. See http://www.vtsoftware.co.uk/tools/circular.htm

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    Quote Originally Posted by DataMiser View Post
    Did you see post #6 seems like that may well be your issue.
    yes,
    that's just a mistake of mine

  16. #16
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,625

    Re: How to end my program properly?

    I had a similar crash-on-exit in the compiled exe but not IDE a few weeks ago; a COM interface wasn't destroyed automatically, and could only be released such that there was no crash with a CopyMemory call... I had
    Dim pStrm As IStream
    'etcetc

    which is then normally freed when the procedure exits or Set = Nothing, but this had to be freed with CopyMemory pStrm, 0&, 4&. You might need to look for objects that haven't been freed.

    That you're getting an error only in the compiled exe instead of simply not unloading makes me lean towards external memory issue like that instead of a simpler form-not-unloaded oversight.

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2016
    Posts
    753

    Re: How to end my program properly?

    fafalone
    Thanks a lot
    solved

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