@@ -2342,7 +2342,8 @@ TPythonModule = class(TMethodsContainer)
2342
2342
procedure DefineDocString ;
2343
2343
procedure Initialize ; override;
2344
2344
procedure InitializeForNewInterpreter ;
2345
- procedure AddClient ( client : TEngineClient );
2345
+ procedure AddClient (Client : TEngineClient);
2346
+ procedure RemoveClient (Client : TEngineClient);
2346
2347
function ErrorByName ( const AName : AnsiString ) : TError;
2347
2348
procedure RaiseError ( const error, msg : AnsiString );
2348
2349
procedure RaiseErrorFmt ( const error, format : AnsiString; const Args : array of const );
@@ -5387,7 +5388,7 @@ procedure TPythonEngine.RemoveClient( client : TEngineClient );
5387
5388
// is not predictable and may cause some memory crashes !
5388
5389
if (csDesigning in ComponentState) then
5389
5390
FClients.Remove( client )
5390
- else if ( Initialized) then begin
5391
+ else if Initialized then begin
5391
5392
FClients.Remove( client );
5392
5393
if (ClientCount = 0 ) then
5393
5394
Finalize;
@@ -7282,9 +7283,10 @@ procedure TPythonModule.InitializeForNewInterpreter;
7282
7283
end ;
7283
7284
end ;
7284
7285
7285
- procedure TPythonModule.AddClient ( client : TEngineClient );
7286
+ procedure TPythonModule.AddClient (Client : TEngineClient);
7286
7287
begin
7287
- FClients.Add( client );
7288
+ if FClients.IndexOf(Client) < 0 then
7289
+ FClients.Add(Client);
7288
7290
end ;
7289
7291
7290
7292
function TPythonModule.ErrorByName ( const AName : AnsiString ) : TError;
@@ -7315,6 +7317,12 @@ procedure TPythonModule.RaiseErrorObj( const error, msg : AnsiString; obj : PPyO
7315
7317
ErrorByName( error ).RaiseErrorObj( msg, obj );
7316
7318
end ;
7317
7319
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
+
7318
7326
procedure TPythonModule.BuildErrors ;
7319
7327
var
7320
7328
i : Integer;
@@ -7984,13 +7992,21 @@ procedure TPythonType.SetModule( val : TPythonModule );
7984
7992
begin
7985
7993
if val <> FModule then
7986
7994
begin
7995
+ if Assigned(FModule) then
7996
+ begin
7997
+ FModule.RemoveFreeNotification(Self);
7998
+ FModule.RemoveClient(Self);
7999
+ end ;
7987
8000
FModule := val;
7988
8001
if Assigned(val) then
8002
+ begin
8003
+ val.FreeNotification(Self);
7989
8004
if Initialized and not (csLoading in ComponentState) then
7990
8005
if val.Initialized then
7991
8006
AddTypeVar
7992
8007
else
7993
8008
val.AddClient(Self);
8009
+ end ;
7994
8010
end ;
7995
8011
end ;
7996
8012
@@ -8608,6 +8624,7 @@ destructor TPythonType.Destroy;
8608
8624
begin
8609
8625
if gVarType = Self then
8610
8626
gVarType := nil ;
8627
+ Module := nil ;
8611
8628
FDocString.Free;
8612
8629
FServices.Free;
8613
8630
inherited ;
0 commit comments