diff --git a/rtl/inc/heap.inc b/rtl/inc/heap.inc index bdc11f5c08..0d4f09642a 100644 --- a/rtl/inc/heap.inc +++ b/rtl/inc/heap.inc @@ -1313,4 +1313,26 @@ begin fillchar(internal_status,sizeof(internal_status),0); end; + +procedure FinalizeHeap; +var + poc : poschunk; + i : longint; +begin +{$ifdef HAS_SYSOSFREE} + for i:=low(freelists_free_chunk) to high(freelists_free_chunk) do + if freelists_free_chunk[i] then + SysOSFree(freelists_fixed[i]^.poc,freelists_fixed[i]^.poc^.size); + while assigned(freeoslist) do + begin + poc:=freeoslist^.next; + SysOSFree(freeoslist, freeoslist^.size); + dec(freeoslistcount); + freeoslist:=poc; + end; +{$endif HAS_SYSOSFREE} + { release mutex } + MemoryMutexManager.MutexDone; +end; + {$endif HAS_MEMORYMANAGER} diff --git a/rtl/inc/system.inc b/rtl/inc/system.inc index 0535ae4639..8952dbb203 100644 --- a/rtl/inc/system.inc +++ b/rtl/inc/system.inc @@ -663,10 +663,12 @@ end; *****************************************************************************} Procedure system_exit;forward; +Procedure FinalizeHeap;forward; Procedure InternalExit; var current_exit : Procedure; + i : longint; Begin while exitProc<>nil Do Begin @@ -691,6 +693,12 @@ Begin Flush(ErrOutput); Flush(StdOut); Flush(StdErr); + { finally release the heap if possible, especially + important for DLLs } + for i:=0 to argc do + sysfreemem(argv[i]); + sysfreemem(argv); + FinalizeHeap; End; diff --git a/rtl/win32/system.pp b/rtl/win32/system.pp index 908647fcce..61c14ce0c6 100644 --- a/rtl/win32/system.pp +++ b/rtl/win32/system.pp @@ -377,7 +377,11 @@ begin begin Close(stderr); Close(stdout); + Close(erroutput); + Close(Input); + Close(Output); { what about Input and Output ?? PM } + { now handled, FPK } end; remove_exception_handlers; @@ -466,7 +470,7 @@ var end; DLL_THREAD_ATTACH : begin - inc(Thread_count); + inclocked(Thread_count); {$warning Allocate Threadvars !} if assigned(Dll_Thread_Attach_Hook) then Dll_Thread_Attach_Hook(DllParam); @@ -474,7 +478,7 @@ var end; DLL_THREAD_DETACH : begin - dec(Thread_count); + declocked(Thread_count); if assigned(Dll_Thread_Detach_Hook) then Dll_Thread_Detach_Hook(DllParam); {$warning Release Threadvars !} @@ -484,9 +488,7 @@ var begin Dll_entry:=true; { return value is ignored } If SetJmp(DLLBuf) = 0 then - begin - FPC_DO_EXIT; - end; + FPC_Do_Exit; if assigned(Dll_Process_Detach_Hook) then Dll_Process_Detach_Hook(DllParam); end; @@ -1237,5 +1239,5 @@ begin errno:=0; initvariantmanager; initwidestringmanager; - InitWin32Widestrings + InitWin32Widestrings; end. diff --git a/rtl/win64/system.pp b/rtl/win64/system.pp index 82e30b3f90..dd104e905b 100644 --- a/rtl/win64/system.pp +++ b/rtl/win64/system.pp @@ -458,7 +458,7 @@ var end; DLL_THREAD_ATTACH : begin - inc(Thread_count); + inclocked(Thread_count); {$warning Allocate Threadvars !} if assigned(Dll_Thread_Attach_Hook) then Dll_Thread_Attach_Hook(DllParam); @@ -466,7 +466,7 @@ var end; DLL_THREAD_DETACH : begin - dec(Thread_count); + declocked(Thread_count); if assigned(Dll_Thread_Detach_Hook) then Dll_Thread_Detach_Hook(DllParam); {$warning Release Threadvars !} diff --git a/rtl/wince/system.pp b/rtl/wince/system.pp index d2f97d909d..12d8fbc42a 100644 --- a/rtl/wince/system.pp +++ b/rtl/wince/system.pp @@ -220,17 +220,17 @@ end; {$ifdef CPUARM} {$define FPC_SYSTEM_HAS_INT} -function floor(d : double) : double; +function floor(d : double) : double; cdecl;external 'coredll' name 'floor'; -function ceil(d : double) : double; +function ceil(d : double) : double; cdecl;external 'coredll' name 'ceil'; function fpc_int_real(d: ValReal): ValReal;compilerproc; begin if d > 0 then fpc_int_real:=floor(d) - else + else fpc_int_real:=ceil(d); end; @@ -564,7 +564,7 @@ var Fargc: longint; Fargv: ppchar; FCmdLine: PChar; - + procedure setup_arguments; var arglen, @@ -814,10 +814,13 @@ begin if IsLibrary then exit; // ExitDLL(ExitCode); - if not IsConsole then begin - Close(stderr); - Close(stdout); - { what about Input and Output ?? PM } + if not IsConsole then + begin + Close(stderr); + Close(stdout); + Close(erroutput); + Close(Input); + Close(Output); end; ExitThread(exitcode); end; @@ -859,21 +862,21 @@ begin end; DLL_THREAD_ATTACH : begin - inc(Thread_count); + inclocked(Thread_count); {$warning Allocate Threadvars !} if assigned(Dll_Thread_Attach_Hook) then Dll_Thread_Attach_Hook(DllParam); end; DLL_THREAD_DETACH : begin - dec(Thread_count); + declocked(Thread_count); if assigned(Dll_Thread_Detach_Hook) then Dll_Thread_Detach_Hook(DllParam); {$warning Release Threadvars !} end; DLL_PROCESS_DETACH : begin - FPC_DO_EXIT; + FPC_Do_Exit; if assigned(Dll_Process_Detach_Hook) then Dll_Process_Detach_Hook(DllParam); end;