Skip to content

Commit 0816040

Browse files
committed
Cleanup module notifications for TPythonType and TDelphiWrapper.
1 parent 424b8e0 commit 0816040

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Source/PythonEngine.pas

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,8 @@ TPythonModule = class(TMethodsContainer)
23422342
procedure DefineDocString;
23432343
procedure Initialize; override;
23442344
procedure InitializeForNewInterpreter;
2345-
procedure AddClient( client : TEngineClient );
2345+
procedure AddClient(Client : TEngineClient);
2346+
procedure RemoveClient(Client : TEngineClient);
23462347
function ErrorByName( const AName : AnsiString ) : TError;
23472348
procedure RaiseError( const error, msg : AnsiString );
23482349
procedure RaiseErrorFmt( const error, format : AnsiString; const Args : array of const );
@@ -5387,7 +5388,7 @@ procedure TPythonEngine.RemoveClient( client : TEngineClient );
53875388
// is not predictable and may cause some memory crashes !
53885389
if (csDesigning in ComponentState) then
53895390
FClients.Remove( client )
5390-
else if (Initialized) then begin
5391+
else if Initialized then begin
53915392
FClients.Remove( client );
53925393
if (ClientCount = 0) then
53935394
Finalize;
@@ -7282,9 +7283,10 @@ procedure TPythonModule.InitializeForNewInterpreter;
72827283
end;
72837284
end;
72847285

7285-
procedure TPythonModule.AddClient( client : TEngineClient );
7286+
procedure TPythonModule.AddClient(Client : TEngineClient);
72867287
begin
7287-
FClients.Add( client );
7288+
if FClients.IndexOf(Client) < 0 then
7289+
FClients.Add(Client);
72887290
end;
72897291

72907292
function TPythonModule.ErrorByName( const AName : AnsiString ) : TError;
@@ -7315,6 +7317,12 @@ procedure TPythonModule.RaiseErrorObj( const error, msg : AnsiString; obj : PPyO
73157317
ErrorByName( error ).RaiseErrorObj( msg, obj );
73167318
end;
73177319

7320+
procedure TPythonModule.RemoveClient(Client: TEngineClient);
7321+
begin
7322+
// Remove does not raise an exception if not found
7323+
FClients.Remove(Client);
7324+
end;
7325+
73187326
procedure TPythonModule.BuildErrors;
73197327
var
73207328
i : Integer;
@@ -7984,13 +7992,21 @@ procedure TPythonType.SetModule( val : TPythonModule );
79847992
begin
79857993
if val <> FModule then
79867994
begin
7995+
if Assigned(FModule) then
7996+
begin
7997+
FModule.RemoveFreeNotification(Self);
7998+
FModule.RemoveClient(Self);
7999+
end;
79878000
FModule := val;
79888001
if Assigned(val) then
8002+
begin
8003+
val.FreeNotification(Self);
79898004
if Initialized and not (csLoading in ComponentState) then
79908005
if val.Initialized then
79918006
AddTypeVar
79928007
else
79938008
val.AddClient(Self);
8009+
end;
79948010
end;
79958011
end;
79968012

@@ -8608,6 +8624,7 @@ destructor TPythonType.Destroy;
86088624
begin
86098625
if gVarType = Self then
86108626
gVarType := nil;
8627+
Module := nil;
86118628
FDocString.Free;
86128629
FServices.Free;
86138630
inherited;

Source/WrapDelphi.pas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,7 @@ destructor TPyDelphiWrapper.Destroy;
36793679
FreeAndNil(fClassRegister);
36803680
FreeAndNil(fHelperClassRegister);
36813681
FreeAndNil(fEventHandlerList);
3682+
Module := nil;
36823683

36833684
//No need to free PythonType objects since they are owned;
36843685
inherited;
@@ -3835,7 +3836,10 @@ procedure TPyDelphiWrapper.SetModule(const Value: TPythonModule);
38353836
begin
38363837
if Value <> FModule then begin
38373838
if Assigned(FModule) then
3839+
begin
38383840
FModule.RemoveFreeNotification(Self);
3841+
FModule.RemoveClient(Self);
3842+
end;
38393843
FModule := Value;
38403844
if Assigned(FModule) then
38413845
FModule.FreeNotification(Self);

0 commit comments

Comments
 (0)