Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
LennertP opened this issue Jul 18, 2024 · 0 comments
Closed
Labels

Comments

@LennertP
Copy link

LennertP commented Jul 18, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants