Skip to content

Calling C# constructors directly from Python results in memory leak #2089

@rzindler

Description

@rzindler

Environment

  • Pythonnet version: 3.01
  • Python version: 3.7, 3.8, 3.9
  • Operating System: Windows/Linux
  • .NET Runtime: .NET Standard 2.0

Details

If I create a C# object in python directly from its constructor, the reference count on the python object is 1 too high.

test = TestClass()
print (f'test ref={sys.getrefcount(test)}')

will print 3 (it should be 2, since getrefcount itself takes a reference)

If, instead, I create a static function of TestClass, that just returns a new TestClass, all is well.

test = TestClass.Create()
print (f'test ref={sys.getrefcount(test)}')

will return 2

The consequence is that objects created by calling a constructor directly are never freed, so large memory leaks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions