Problem mit stack overflow
-
Hallo ich habe ein Problem und bin am verzweifeln, es liegt an der Switch case anweisung, weil wenn ich diese auskommentiere geht das programm, aber sobald ich es laufen lasse, kommt der Fehler stack overflow... ich weiß nicht mehr weiter

Kann mir einer die Lösung veraten mir fallen schon die Haare raus
code :
CString strAusgabe; CString strSpannung, strStrom, strWiderstand; float fSpannung, fStrom, fWiderstand; switch(m_cmbKombo1.GetCurSel()) { case 0: // U=R*I // Zuweisen der Bezeichnungen m_stat1.SetWindowText("Widerstand [Ohm]"); m_stat2.SetWindowText("Strom [A]"); m_stat3.SetWindowText("Spannung [V]"); // m_ctlEdit3. GetWindowText (strSpannung ); m_ctlEdit2.GetWindowText (strStrom ); m_ctlEdit1. GetWindowText (strWiderstand); // fSpannung = atof(strSpannung ); fStrom = atof(strStrom ); fWiderstand = atof(strWiderstand); strAusgabe.Format("U = %f V",fStrom * fWiderstand); m_ctlEdit3.SetWindowText(strAusgabe); break; case 1: // I=U/R // Zuweisen der Bezeichnungen m_stat1.SetWindowText("Spannung [V]"); m_stat2.SetWindowText("Widerstand [Ohm]"); m_stat3.SetWindowText("Strom [A]"); // Ergebnis m_ctlEdit1. GetWindowText (strSpannung ); // m_ctrlEdit3.GetWindowText (strStrom ); m_ctlEdit2. GetWindowText (strWiderstand); fSpannung = atof(strSpannung ); // fStrom = atof(strStrom ); fWiderstand = atof(strWiderstand); if (fWiderstand>=0.0000001) strAusgabe.Format("I = %f V",fSpannung / fWiderstand); else strAusgabe.Format("Darf man NICHT"); m_ctlEdit3.SetWindowText(strAusgabe); break; case 2: // R=U/I // Zuweisen der Bezeichnungen m_stat1.SetWindowText("Spannung [V]"); m_stat2.SetWindowText("Strom [A]"); m_stat3.SetWindowText("Widerstand [Ohm]"); m_ctlEdit1. GetWindowText (strSpannung ); m_ctlEdit2.GetWindowText (strStrom ); //m_ctlEdit3. GetWindowText (strWiderstand); fSpannung = atof(strSpannung ); fStrom = atof(strStrom ); // fWiderstand = atof(strWiderstand); if (fStrom>=0.0000001) strAusgabe.Format("R = %f V",fSpannung / fStrom); else strAusgabe.Format("Darf man NICHT"); m_ctlEdit3.SetWindowText(strAusgabe); break;*/ default: //AfxMessageBox("Bitte stellen Sie den Computer ab."); break; } m_ctlEdit3.SetWindowText(strAusgabe); }
-
Kann es sein, dass Du diesen Code in einem EN_CHANGE Handler ausführst?
In diesem Fall wird durch ein SetWindowText ein neuer EN_CHANGE ausgelöst was zur Rekursion führt. Ist dies so? Dann setze eine Variable die den rekursiven Aufruf verhindert.
Nützlich bei einem Stackoverflow ist ein Blick im Debugger auf den Callstack!
-
mein callstack ist ziemlich lang
und mit welcher variable sollte ich den rekrusiven aufruf verhindern ?
-
ice101 schrieb:
mein callstack ist ziemlich lang
und mit welcher variable sollte ich den rekrusiven aufruf verhindern ?
Mit einer eigenen in der Klasse...
-
einfach eine statische Variable deklarieren
void OnChange () { static bNotAtWork = true; if (bNotAtWork) { bNotAtWork = false; ... SetWindowText (...); // d.h. erneut OnChange () ... bNotAtWork = true; } }
-
Statische Variablen sind für diesen Fall nicht gut. Besser man baut die Variable in der Klasse ein.