split OnDestroy across the appropriate modules (this doen't cause leaks on mobile, right?)
authorkoda
Sat, 05 May 2012 17:29:04 +0100
changeset 7026 8d1724e1337e
parent 7025 831a25c74c7b
child 7027 f264ad9d8965
split OnDestroy across the appropriate modules (this doen't cause leaks on mobile, right?)
hedgewars/hwengine.pas
hedgewars/uAI.pas
hedgewars/uIO.pas
hedgewars/uInputHandler.pas
hedgewars/uStore.pas
--- a/hedgewars/hwengine.pas	Sat May 05 17:19:33 2012 +0100
+++ b/hedgewars/hwengine.pas	Sat May 05 17:29:04 2012 +0100
@@ -42,7 +42,6 @@
 
 implementation
 {$ELSE}
-procedure OnDestroy; forward;
 procedure initEverything(complete:boolean); forward;
 procedure freeEverything(complete:boolean); forward;
 {$ENDIF}
@@ -124,24 +123,6 @@
         end;
 end;
 
-////////////////////
-procedure OnDestroy;
-begin
-    WriteLnToConsole('Freeing resources...');
-    FreeActionsList();
-    StoreRelease(false);
-    ControllerClose();
-    CloseIPC();
-    TTF_Quit();
-{$IFDEF SDL13}
-    SDL_GL_DeleteContext(SDLGLcontext);
-    SDL_DestroyWindow(SDLwindow);
-    SDLGLcontext:= nil;
-    SDLwindow:= nil;
-{$ENDIF}
-    SDL_Quit();
-end;
-
 ///////////////////
 procedure MainLoop;
 var event: TSDL_Event;
@@ -383,7 +364,6 @@
         LoadRecordFromFile(recordFileName);
 
     ScriptOnGameInit;
-
     s:= 'eproto ' + inttostr(cNetProtoVersion);
     SendIPCRaw(@s[0], Length(s) + 1); // send proto version
 
@@ -394,12 +374,9 @@
     isDeveloperMode:= false;
     TryDo(InitStepsFlags = cifAllInited, 'Some parameters not set (flags = ' + inttostr(InitStepsFlags) + ')', true);
     ParseCommand('rotmask', true);
-
     MainLoop();
 
-    // clean up SDL and GL context
-    OnDestroy();
-    // clean up all the other memory allocated
+    // clean up all the memory allocated
     freeEverything(true);
     if alsoShutdownFrontend then
         halt;
@@ -414,21 +391,19 @@
     else
         cLogfileBase:= 'preview';
 
-    // uConsts does not need initialization as they are all consts
-    uUtils.initModule;
+    uUtils.initModule;      // this opens the debug file, must be the first
     uMisc.initModule;
     uVariables.initModule;
-    uConsole.initModule;    // MUST happen after uMisc
+    uConsole.initModule;
     uCommands.initModule;
     uCommandHandlers.initModule;
 
     uLand.initModule;
     uLandPainted.initModule;
-
     uIO.initModule;
 
     if complete then
-        begin
+    begin
 {$IFDEF ANDROID}GLUnit.init;{$ENDIF}
 {$IFDEF SDL13}uTouch.initModule;{$ENDIF}
         uAI.initModule;
@@ -456,18 +431,21 @@
         uVisualGears.initModule;
         uWorld.initModule;
         uCaptions.initModule;
-        end;
+    end;
 end;
 
 procedure freeEverything (complete:boolean);
 begin
     if complete then
-        begin
+    begin
+        WriteLnToConsole('Freeing resources...');
+        uAI.freeModule;
+        uAIMisc.freeModule;         //stub
         uCaptions.freeModule;
         uWorld.freeModule;
         uVisualGears.freeModule;
         uTeams.freeModule;
-        uStore.freeModule;          //stub
+        uInputHandler.freeModule;
         uStats.freeModule;          //stub
         uSound.freeModule;
         uScript.freeModule;
@@ -477,20 +455,18 @@
         uLandTexture.freeModule;
         //uLandObjects does not need to be freed
         //uLandGraphics does not need to be freed
-        uInputHandler.freeModule;           //stub
         uGears.freeModule;
         //uGame does not need to be freed
         //uFloat does not need to be freed
         uCollisions.freeModule;     //stub
         uChat.freeModule;
         uAmmos.freeModule;
-        uAIMisc.freeModule;         //stub
         //uAIAmmoTests does not need to be freed
         //uAIActions does not need to be freed
-        uAI.freeModule;             //stub
-        end;
+        uStore.freeModule;
+    end;
 
-    uIO.freeModule;             //stub
+    uIO.freeModule;
     uLand.freeModule;
     uLandPainted.freeModule;
 
@@ -504,8 +480,7 @@
 
 /////////////////////////
 procedure GenLandPreview{$IFDEF HWLIBRARY}(port: LongInt); cdecl; export{$ENDIF};
-var
-    Preview: TPreview;
+var Preview: TPreview;
 begin
     initEverything(false);
 {$IFDEF HWLIBRARY}
@@ -522,7 +497,6 @@
     SendIPCRaw(@Preview, sizeof(Preview));
     SendIPCRaw(@MaxHedgehogs, sizeof(byte));
     WriteLnToConsole('Preview sent, disconnect');
-    CloseIPC();
     freeEverything(false);
 end;
 
--- a/hedgewars/uAI.pas	Sat May 05 17:19:33 2012 +0100
+++ b/hedgewars/uAI.pas	Sat May 05 17:29:04 2012 +0100
@@ -45,23 +45,23 @@
 
 procedure FreeActionsList;
 begin
-AddFileLog('FreeActionsList called');
-if hasThread <> 0 then
+    AddFileLog('FreeActionsList called');
+    if hasThread <> 0 then
     begin
-    AddFileLog('Waiting AI thread to finish');
-    StopThinking:= true;
-    repeat
-        SDL_Delay(10)
-    until hasThread = 0
+        AddFileLog('Waiting AI thread to finish');
+        StopThinking:= true;
+        repeat
+            SDL_Delay(10)
+        until hasThread = 0
     end;
 
-with CurrentHedgehog^ do
-    if Gear <> nil then
-        if BotLevel <> 0 then
-            StopMessages(Gear^.Message);
+    with CurrentHedgehog^ do
+        if Gear <> nil then
+            if BotLevel <> 0 then
+                StopMessages(Gear^.Message);
 
-BestActions.Count:= 0;
-BestActions.Pos:= 0
+    BestActions.Count:= 0;
+    BestActions.Pos:= 0
 end;
 
 
@@ -445,7 +445,7 @@
 
 procedure freeModule;
 begin
-
+    FreeActionsList();
 end;
 
 end.
--- a/hedgewars/uIO.pas	Sat May 05 17:19:33 2012 +0100
+++ b/hedgewars/uIO.pas	Sat May 05 17:29:04 2012 +0100
@@ -25,6 +25,7 @@
 procedure initModule;
 procedure freeModule;
 
+procedure InitIPC;
 procedure SendIPC(s: shortstring);
 procedure SendIPCc(c: char);
 procedure SendIPCXY(cmd: char; X, Y: SmallInt);
@@ -35,8 +36,6 @@
 procedure SendStat(sit: TStatInfoType; s: shortstring);
 procedure IPCWaitPongEvent;
 procedure IPCCheckSock;
-procedure InitIPC;
-procedure CloseIPC;
 procedure NetGetNextCmd;
 procedure doPut(putX, putY: LongInt; fromAI: boolean);
 
@@ -112,13 +111,6 @@
     WriteLnToConsole(msgOK)
 end;
 
-procedure CloseIPC;
-begin
-    SDLNet_FreeSocketSet(fds);
-    SDLNet_TCP_Close(IPCSock);
-    SDLNet_Quit();
-end;
-
 procedure ParseIPCCommand(s: shortstring);
 var loTicks: Word;
 begin
@@ -443,7 +435,10 @@
 
 procedure freeModule;
 begin
-while headcmd <> nil do RemoveCmd
+    while headcmd <> nil do RemoveCmd;
+    SDLNet_FreeSocketSet(fds);
+    SDLNet_TCP_Close(IPCSock);
+    SDLNet_Quit();
 end;
 
 end.
--- a/hedgewars/uInputHandler.pas	Sat May 05 17:19:33 2012 +0100
+++ b/hedgewars/uInputHandler.pas	Sat May 05 17:29:04 2012 +0100
@@ -39,7 +39,6 @@
 procedure SetDefaultBinds;
 
 procedure ControllerInit;
-procedure ControllerClose;
 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
 procedure ControllerHatEvent(joy, hat, value: Byte);
 procedure ControllerButtonEvent(joy, button: Byte; pressed: Boolean);
@@ -419,14 +418,6 @@
     WriteLnToConsole('Not using any game controller');
 end;
 
-procedure ControllerClose;
-var j: Integer;
-begin
-    if ControllerEnabled > 0 then
-        for j:= 0 to pred(ControllerNumControllers) do
-            SDL_JoystickClose(Controller[j]);
-end;
-
 procedure ControllerAxisEvent(joy, axis: Byte; value: Integer);
 begin
     ControllerAxes[joy][axis]:= value;
@@ -452,8 +443,12 @@
 end;
 
 procedure freeModule;
+var j: LongInt;
 begin
-
+    // close gamepad controllers
+    if ControllerEnabled > 0 then
+        for j:= 0 to pred(ControllerNumControllers) do
+            SDL_JoystickClose(Controller[j]);
 end;
 
 end.
--- a/hedgewars/uStore.pas	Sat May 05 17:19:33 2012 +0100
+++ b/hedgewars/uStore.pas	Sat May 05 17:29:04 2012 +0100
@@ -1141,6 +1141,13 @@
 
 procedure freeModule;
 begin
+    StoreRelease(false);
+    TTF_Quit();
+{$IFDEF SDL13}
+    SDL_GL_DeleteContext(SDLGLcontext);
+    SDL_DestroyWindow(SDLwindow);
+{$ENDIF}
+    SDL_Quit();
 end;
 
 end.