1

I have some sort of following code in my windows form application (I am basically a backend api developer doing some small app support of winform app at the moment)

var form2 = new frmTicketsList(this);
form2.Closed += (s, args) => this.Close();
form2.Show();
this.Hide();

Every now and then I am getting a memory overflow exception at line

form2.Closed += (s, args) => this.Close();

Any ideas to what the problem could be?

Neuron
  • 5,141
  • 5
  • 38
  • 59
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • 1
    Pretty obvious: `this.Close()` invokes the Closed event, which in turn .... What were you trying to accomplish? – 500 - Internal Server Error Oct 11 '21 at 11:50
  • The form `frmTicketsList` is that the same form as the one you are executing code within? Also, depending on where you are creating the `new frmTicketsList` (e.g in the Constructor) you will be creating infinite new instances of the same form which in-tern creates more instances, etc... – ConnorTJ Oct 11 '21 at 11:52
  • frmTicketList is not the same form. Basically i need to preserve the state that's why i am passing my form current form in next form. there on some previous button click i navigate back to the old form with state intact. – Kamran Shahid Oct 11 '21 at 11:55
  • @500-InternalServerError on close click i am basically closing old form from which current form is being navigated. solution is as mentioned in https://stackoverflow.com/questions/5548746/c-sharp-open-a-new-form-then-close-the-current-form – Kamran Shahid Oct 11 '21 at 11:56
  • Have you taken a look at Singletons (https://csharpindepth.com/articles/singleton) to help you manage state of data between forms? Might be a better approach than opening and closing forms and trying to manage state mutations between them, especially when the application grows. – ConnorTJ Oct 11 '21 at 11:58
  • I am not just trying out with replacing this.Close(); with { Application.Exit(); }; for quick solution. form2.Closed += (s, args) => { Application.Exit(); }; Not much expert in windows form development – Kamran Shahid Oct 11 '21 at 12:03
  • Form class has no parameterized constructor, so is frmTicketsList actually a Form? and what it does in the constructor might be the culprit. – tia Oct 11 '21 at 12:53
  • 1
    Stack overflow is usually the result of an infinite loop. What is happening inside frmTicketsList with the parameter you're passing in the constructor? Do you also have a `form2.Closed += (s, args) => this.Close();` statement there too? Then that would explain the infinite loop. Both forms are trying to close each other in an endless circle. To prevent it, remove the event handler on form2.Closed before calling this.close. – jjthebig1 Oct 11 '21 at 13:22

1 Answers1

0

use following approach

 var form2 = new frmCustomerSearchWizard();
                                form2.Closed += (s, args) => { Application.Exit(); };
                                form2.Show();
                                this.Hide();
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93