How to deal with objects that live in C++ and Python?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
andrecaldas
Posts: 300
Joined: Fri Jan 27, 2023 8:45 pm
Contact:

How to deal with objects that live in C++ and Python?

Post by andrecaldas »

I am experimenting a little with the Sketcher code.

Using python, when you add a constraint to the Sketcher, you create it and pass it to sketch.addConstraint(). The C++ counterpart of the addConstraint() method duplicates (clones) the constraint to an instance that does not have a python counter part.

I am trying to simply keep the reference instead of copying it. But I don't want to get a dangling reference when the python object is destructed. I realize that I probably have to increase some reference counter somewhere.

So, I would like to ask what is the proper way to do that. What is the proper way to deal with references to python objects and avoid them getting garbage collected?
Post Reply