浏览代码

Allow freeing EHeapException and its descendants.

Rika Ichinose 3 月之前
父节点
当前提交
5952c5452b
共有 2 个文件被更改,包括 8 次插入16 次删除
  1. 1 4
      rtl/objpas/sysutils/sysutilh.inc
  2. 7 12
      rtl/objpas/sysutils/sysutils.inc

+ 1 - 4
rtl/objpas/sysutils/sysutilh.inc

@@ -161,10 +161,7 @@ type
    end;
 
    EHeapMemoryError = class(Exception)
-     protected
-       AllowFree : boolean;
-     public
-       procedure FreeInstance;override;
+     procedure FreeInstance;override;
    end;
 
    EHeapException = EHeapMemoryError;

+ 7 - 12
rtl/objpas/sysutils/sysutils.inc

@@ -253,10 +253,13 @@ end;
       Result:=ClassName+': '+Message;
     end;
 
+Var OutOfMemory : EOutOfMemory;
+    InValidPointer : EInvalidPointer;
+
     procedure EHeapMemoryError.FreeInstance;
     begin
-       if AllowFree then
-        inherited FreeInstance;
+       if (pointer(self)<>pointer(OutOfMemory)) and (pointer(self)<>pointer(InvalidPointer)) then
+         inherited FreeInstance;
     end;
 
 
@@ -429,10 +432,6 @@ begin
   result:=nil;
 end;
 
-Var OutOfMemory : EOutOfMemory;
-    InValidPointer : EInvalidPointer;
-
-
 Procedure RunErrorToExcept (ErrNo : Longint; Address : CodePointer; Frame : Pointer);
 var
   E: Exception;
@@ -548,9 +547,7 @@ begin
   ExceptProc:=@CatchUnhandledException;
   // Create objects that may have problems when there is no memory.
   OutOfMemory:=EOutOfMemory.Create(SOutOfMemory);
-  OutOfMemory.AllowFree:=false;
   InvalidPointer:=EInvalidPointer.Create(SInvalidPointer);
-  InvalidPointer.AllowFree:=false;
   AssertErrorProc:=@AssertErrorHandler;
   ErrorProc:=@RunErrorToExcept;
   OnShowException:=Nil;
@@ -559,10 +556,8 @@ end;
 
 Procedure DoneExceptions;
 begin
-  OutOfMemory.AllowFree:=true;
-  OutOfMemory.Free;
-  InValidPointer.AllowFree:=true;
-  InValidPointer.Free;
+  FreeAndNil(OutOfMemory); { These will automatically work as they first set pointer to nil, then free the object. See EHeapMemoryError.FreeInstance. }
+  FreeAndNil(InValidPointer);
 end;