@@ -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 ;
72837284end ;
72847285
7285- procedure TPythonModule.AddClient ( client : TEngineClient );
7286+ procedure TPythonModule.AddClient (Client : TEngineClient);
72867287begin
7287- FClients.Add( client );
7288+ if FClients.IndexOf(Client) < 0 then
7289+ FClients.Add(Client);
72887290end ;
72897291
72907292function 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 );
73167318end ;
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+
73187326procedure TPythonModule.BuildErrors ;
73197327var
73207328 i : Integer;
@@ -7984,13 +7992,21 @@ procedure TPythonType.SetModule( val : TPythonModule );
79847992begin
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 ;
79958011end ;
79968012
@@ -8608,6 +8624,7 @@ destructor TPythonType.Destroy;
86088624begin
86098625 if gVarType = Self then
86108626 gVarType := nil ;
8627+ Module := nil ;
86118628 FDocString.Free;
86128629 FServices.Free;
86138630 inherited ;
0 commit comments