Skip to content

TPythonType.NewSubtypeInst() destroys the object twice in case of errorΒ #481

@LennertP

Description

@LennertP

Recently PythonEngine.pas was modified to include 'obj.Free' when Engine.PyErr_Occurred indicates an error. As a result, the newly created object will be destroyed twice as Engine.Py_DECREF(Result) will also call the destructor in PyObjectDestructor() (in this code 'obj' and 'Result' are pointing to the same object following 'obj := PythonToDelphi(Result)').

Here is the relevant code excerpt:

function  TPythonType.NewSubtypeInst( aType: PPyTypeObject; args, kwds : PPyObject) : PPyObject;
var
  obj : TPyObject;
begin
(..)
  if Assigned(Result) then
  begin
    obj := PythonToDelphi(Result);
    (..)
    if Engine.PyErr_Occurred <> nil then
    begin
      Engine.Py_DECREF(Result);
      Result := nil;
      obj.Free; // <- This statement is redundant and calls the destructor for the 2nd time
    end;
  end;
end;

My proposal is to remove the redundant/erroneous call to obj.Free above. Do you agree?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions