StackOverflow followed by a lot of closing forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SvenV
    New Member
    • Oct 2008
    • 50

    StackOverflow followed by a lot of closing forms

    Hello,

    Currently I'm developing an application for a Windows CE 4.0 device. This application contains a lot of different forms. The application jumps from one form to another. And I close the forms when I go to another form but I don't think it's the best way to close them.
    Sometimes, at random moments, I receive a StackOverflowEx ception and on the device I see a lot of forms closing, you see the behavior that you normally get when you form a close. So I guess it's because there are still too many forms open to manage.

    When I go from one form to another I do it like this.
    this.close();
    Form1 frm1 = new Form1();
    frm1.ShowDialog ();

    The problem is that the current form isn't closed right away. It's still there somewhere..Only when the new form is closed the older form is closed..

    It feels like this is a stupid problem, sounds easy to fix but I can't think of a fix right away. Is there a proper way of doing form management withing a Windows CE application.

    For the record, I'm developing using .NET1.1

    Kind regards,
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    #2
    It looks like you are creating a child form belonging to the parent form and then trying to close the parent form. But this isn't really happy as there is obviously a child attached (i.e. what would happen if on the child you wrote
    Code:
    this.parent;
    )

    You need to consider how your application is structured. Like running the main program on a thread, then launching the forms off that. Then you can close forms and create new forms off the thread and not each other.

    Unfortunately I am not saying that this is the best way to do it, it's just a suggestion. You will probably find that when you are "closing" your forms they are still allocated memory and resources until you close all the child forms belonging to them and won't be disposed properly until they have no dependencies.

    Comment

    • SvenV
      New Member
      • Oct 2008
      • 50

      #3
      I think that I what you mean, I don't however not know a correct solution to this problem..

      The problem is that work from one form is linked to another form. In App I launch my first form.
      Application.Run (new FormUserLogin() );

      From that form It goes on, then user sees like an option screen where it has different options. if i select option one I open FormOption1 for instance and so on...

      So how would I fix this issue?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I would consider having Application.Run () a 'controler' form if you will.
        The controller form is in charge of invoking the opening/closing of forms.
        So when it starts, it will run the FormUserLogin form. Based on what the user has input on that form, the controller form closes it(or the form closes itself and the controller form can see that, whatever) and then opens up the next form.

        Your form structure should then be a single parent node with a lot of leaf nodes, not a full tree structure.
        Like:
        Controler
        / | \ \
        Login Option1 Etc Etc

        Instead of doing this (bad for forms):
        Login
        | \
        GoodLogin BadLogin
        | \
        Option1 Option2
        |
        NextOption

        Comment

        • SvenV
          New Member
          • Oct 2008
          • 50

          #5
          Is there no other way of solving the issue? The forms are nested so deep within each other that I have no clue how to solve it using the above way.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by SvenV
            The forms are nested so deep within each other
            That should be your first clue that you have a serious design flaw.

            There mayb be another way, declaring all their variables as static maybe?
            And calling GC.Collect() ?

            Comment

            • SvenV
              New Member
              • Oct 2008
              • 50

              #7
              Yes I know, but the mess was there before I started working on it ;-)
              GC.Collect() is maybe an idea, but I'm aware of the fact that I need to set this right or it will keep happening. The forms will stay open unless the parent is closed.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Then its a perfect time to refactor the code. Don't be afraid of shaking things up if you inherit crappy code. Somethings are abortions when they were first started, but that doesn't mean they need to continue being that way.

                Plus you should upgrade to a later version of CE. 4.0 is very limited.

                Comment

                • SvenV
                  New Member
                  • Oct 2008
                  • 50

                  #9
                  Yes i know :)
                  But you know how it goes, time&money..
                  This is something however something that needs to be addressed ASAP.

                  I tried opening the solution in VS2005 & VS2008 but the forms came out all wacky. I recreated every form and then it kinda worked but then there was an issue with the localised resources.

                  Comment

                  • RedSon
                    Recognized Expert Expert
                    • Jan 2007
                    • 4980

                    #10
                    This time & money business is a common excuse, however it is a fallacy. It's going cost you and your company 10 times as much struggling and fighting with this code now and the next time it needs to be updated vs. sitting down and thinking about the design and creating it the right way.

                    Comment

                    • markmcgookin
                      Recognized Expert Contributor
                      • Dec 2006
                      • 648

                      #11
                      Try telling that to my boss.... a small achievement now that he can wave infront of a customer makes him happy when the code needs a bloody redesign </rant>

                      Comment

                      • RedSon
                        Recognized Expert Expert
                        • Jan 2007
                        • 4980

                        #12
                        Yes I know... but it is our job as engineers to temper that short term gain for long term achievement. At each phase of development the cost to fix an error becomes exponentially greater. In Sven's case since he is at the final stage of development (maintenance) it is going to cost his company the most to fix the problems he is experiencing. However the company's ROI will be greater if they trash their previous program and design a better one that will hold up better to future development.

                        Comment

                        • SvenV
                          New Member
                          • Oct 2008
                          • 50

                          #13
                          Would the singleton pattern be a possible solution?
                          This way I would only have one instance of the form that would be called.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            I have a rather simple solution that I request that you try.
                            If you display a form using ShowDialog then it won't be Disposed when you call Form.Close. i.e Try calling Form.Dispose() where you are calling Form.Close.

                            Comment

                            Working...