Anwendung beenden



  • Ich möchte eine Anwendung bei Auftreten eines Fehlers ordungsgemäß beenden.
    Leider schaffe ich das nicht bzw. habe einen prinzipiellen Denkfehler

    Folgender Code steht in einer Funktion der Dokumentenklasse

    // open the init-file
    if( !file.Open( str1, CFile::modeRead, &e ) )
    {
    	#ifdef _DEBUG
    	afxDump << "File could not be opened " << e.m_cause << "\n";
    	#endif
    	GetPointerToActiveView()->MessageBox("File urv.dat has not been found. Install now new!");
    	GetPointerToActiveView()->GetParent()->PostMessage(WM_CLOSE);
    	return FALSE;
    }
    

    Hiermit schließt sich zwar das Fenster aber die ANwendung ist noch offen (sichtbar im Taskmanager)
    Gibt es keinen Befehl der eine Click auf das Kreuz rechts oben immitiert?



  • PostQuitMessage() sollte dir weiterhelfen können.



  • Danke,
    das hatte ich vorher schon versucht - die Anwenddung schließt sich zwar komplett, aber ich erhalte eine Menge Memory Leaks - die kommen von den Ansichten:

    Detected memory leaks!
    Dumping objects ->
    strcore.cpp(118) : {207} normal block at 0x00A73180, 58 bytes long.
    Data: < - - Acti> 01 00 00 00 2D 00 00 00 2D 00 00 00 41 63 74 69
    strcore.cpp(118) : {206} normal block at 0x00A731F0, 56 bytes long.
    Data: < + + Acti> 01 00 00 00 2B 00 00 00 2B 00 00 00 41 63 74 69
    plex.cpp(31) : {205} normal block at 0x00A73370, 164 bytes long.
    Data: < 1 > 00 00 00 00 00 00 00 00 0E 00 00 00 FC 31 A7 00
    map_sp.cpp(74) : {204} normal block at 0x00A73440, 68 bytes long.
    Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    strcore.cpp(118) : {182} normal block at 0x00A730E0, 19 bytes long.
    Data: < Bere> 01 00 00 00 06 00 00 00 06 00 00 00 42 65 72 65
    winfrm.cpp(370) : {178} normal block at 0x00A73330, 8 bytes long.
    Data: <H > 48 11 08 00 00 00 00 00
    strcore.cpp(118) : {163} normal block at 0x00A73550, 21 bytes long.
    Data: < Unti> 01 00 00 00 08 00 00 00 08 00 00 00 55 6E 74 69
    C:\Holger\TEST\englsih_v2\Splash.cpp(51) : {141} client block at 0x00A737B0, subtype 0, 72 bytes long.
    a CWnd object at $00A737B0, 72 bytes long
    array_p.cpp(110) : {140} normal block at 0x00A73830, 20 bytes long.
    Data: < - @. > 00 00 00 00 A0 2D A7 00 00 00 00 00 40 2E A7 00
    array_p.cpp(71) : {138} normal block at 0x00A73920, 4 bytes long.
    Data: < > 00 00 00 00
    winfrm2.cpp(66) : {137} client block at 0x00A73950, subtype 0, 168 bytes long.
    a CDockBar object at $00A73950, 168 bytes long
    array_p.cpp(71) : {136} normal block at 0x00A73A30, 4 bytes long.
    Data: < > 00 00 00 00
    winfrm2.cpp(66) : {135} client block at 0x00A73A60, subtype 0, 168 bytes long.
    a CDockBar object at $00A73A60, 168 bytes long
    array_p.cpp(71) : {134} normal block at 0x00A73F10, 4 bytes long.
    Data: < > 00 00 00 00
    winfrm2.cpp(66) : {133} client block at 0x00A73B40, subtype 0, 168 bytes long.
    a CDockBar object at $00A73B40, 168 bytes long
    winfrm2.cpp(66) : {131} client block at 0x00A73C20, subtype 0, 168 bytes long.
    a CDockBar object at $00A73C20, 168 bytes long
    bardock.cpp(735) : {130} normal block at 0x00A73D00, 176 bytes long.
    Data: < K_ > 84 01 4B 5F CD CD CD CD CD CD CD CD CD CD CD CD
    bardock.cpp(735) : {129} normal block at 0x00A73F40, 176 bytes long.
    Data: < K_ > 84 01 4B 5F CD CD CD CD CD CD CD CD CD CD CD CD
    strcore.cpp(118) : {124} normal block at 0x00A73DE0, 15 bytes long.
    Data: < RF > 01 00 00 00 02 00 00 00 02 00 00 00 52 46 00
    strcore.cpp(118) : {123} normal block at 0x00A73E20, 16 bytes long.
    Data: < NUM > 01 00 00 00 03 00 00 00 03 00 00 00 4E 55 4D 00
    strcore.cpp(118) : {122} normal block at 0x00A73E60, 15 bytes long.
    Data: < UF > 01 00 00 00 02 00 00 00 02 00 00 00 55 46 00
    strcore.cpp(118) : {121} normal block at 0x00A73EA0, 44 bytes long.
    Data: < Curr> 01 00 00 00 1F 00 00 00 1F 00 00 00 43 75 72 72
    strcore.cpp(118) : {120} normal block at 0x00A72030, 18 bytes long.
    Data: < > 01 00 00 00 05 00 00 00 05 00 00 00 20 20 20 20
    {117} normal block at 0x00A72070, 120 bytes long.
    Data: < > 00 00 00 00 A4 01 00 00 00 01 00 08 00 00 00 00
    plex.cpp(31) : {112} normal block at 0x00A72120, 124 bytes long.
    Data: < 0! - > 00 00 00 00 30 21 A7 00 00 00 00 00 A0 2D A7 00
    plex.cpp(31) : {102} normal block at 0x00A723D0, 124 bytes long.
    Data: < ' > 00 00 00 00 00 00 00 00 00 00 00 00 00 27 A7 00
    C:\Holger\TEST\englsih_v2\englsih_v2View.cpp(20) : {96} client block at 0x00A72700, subtype 0, 904 bytes long.
    a CEnglsih_v2View object at $00A72700, 904 bytes long
    strcore.cpp(118) : {92} normal block at 0x00A71040, 23 bytes long.
    Data: < engl> 01 00 00 00 0A 00 00 00 0A 00 00 00 65 6E 67 6C
    C:\Holger\TEST\englsih_v2\MainFrm.cpp(27) : {90} client block at 0x00A72CE0, subtype 0, 776 bytes long.
    a CMainFrame object at $00A72CE0, 776 bytes long
    strcore.cpp(118) : {89} normal block at 0x00A710F0, 25 bytes long.
    Data: < unkn> 01 00 00 00 0C 00 00 00 0C 00 00 00 75 6E 6B 6E
    strcore.cpp(118) : {88} normal block at 0x00A71090, 45 bytes long.
    Data: < \\nv> 01 00 00 00 20 00 00 00 20 00 00 00 5C 5C 6E 76
    C:\Holger\TEST\englsih_v2\englsih_v2Doc.cpp(20) : {86} client block at 0x00A71140, subtype 0, 384 bytes long.
    a CEnglsih_v2Doc object at $00A71140, 384 bytes long
    Object dump complete.
    Thread 0x10E0 wurde mit Code 0 (0x0) beendet.
    Das Programm "C:\Holger\TEST\englsih_v2\Debug\englsih_v2.exe" wurde mit Code 0 (0x0) beendet.


  • Mod

    Was heißt ordnungsgemäß?
    Wenn man PostQuitMessage verwendet kann man auch gleich exit verwenden.
    Der einfachste Weg ist vermutlich

    AfxGetMainWnd()->DestroyWindow();
    

    und mit Sicherheit besser als das herum posten von Nachrichten. Auf WM_CLOSE hin, kan das programm immer noch sagen: Ich will nicht!



  • Ich möchte eigentlich nicht mehr und nicht weniger als die Funktion aufrufen, die aufgerufen wird, wenn mann im Menu auf Datei->Beenden klickt bzw. auf das kreuz rechts oben. Ich vermute/hoffe, dass dadurch automatisch die Ansichten geschlossen/zerstört werden bzw. die entsprechenden Klassen des Dokuments aufgerufen werden (DeleteContents, ~CEnglsih_v2Doc(), ...)


  • Mod

    Dann müsstest Du an das Frame Window WM_CLOSE senden.

    AfxGetMainWnd()->SendMessage(WM_CLOSE);
    

    Das führt aber nicht zum Beenden, sondern evtl. auch zu der Frage ob geänderte Daten gespeichert werden sollen.
    Wenn Du wirklich die Applikation final Beenden willst musst Du das Frame Window zerstören.

    AfxGetMainWnd()->DestroyWindow();
    

    Eigentlich ist es noch komplizierter, schau Dir mal den Code von CFrameWnd::OnClose an.



  • Wie beendet ihr denn ein Programm (ohne Anwendereinwirkung), welches ein Fehler aufweißt (z.B. eine Datei fehlt oder Ähliches)


  • Mod

    Brutal: Entweder war es sowieso ein UAE, dann schreibe ich einen Dump und terminiere.
    Oder ich verwende FatalAppExit. Meistens aber versuche ich dann noch evtl. kritische Ressourcen zuvor freizugeben (falls es die gibt).

    Es kommt eben darauf an, wie fatal der Fehler ist. Wenn ich ihn behandeln kann, wird eben eine Exception geworfen.
    Wenn ich z.B. ein Programm habe dass auf eine DB-Verbindung (Backgroundthreads etc.) angewiesen ist, und diese reist ab, ist das für mich ein fataler Fehler mit einem Ende und aus.
    Hat eine Datei beim Lesen ein falsches Format, dann wird eben eine Exception geworfen und die wird behandelt und angezeigt.

    Die Frage ist also: Was ist ein Fehler, der so gravierend ist, dass Du ihn nicht behandeln kannst?
    Hast Du kritische Ressourcen die das System blockieren können? Was kannst Du selber noch gesichert aufräumen?

    Fenster und Speicher gehören eben nicht dazu, die räumt Windows selber auf, wenn der Prozess terminiert!


Anmelden zum Antworten