• Mayank Mittal's avatar
    Fixes the issue with using callbacks inside python classes (#181) · 8ba18efa
    Mayank Mittal authored
    # Description
    
    In Python, memory management is based on reference counts, where an
    object isn't "truly" deleted if it has variables referring to the memory
    address. More details:
    https://www.pythontutorial.net/advanced-python/python-references/
    
    In Orbit code, we provide "self" to various callbacks, which increments
    the reference counts since they "refer" to the object. This means that
    when we do:
    ```python
    robot = Articulation(cfg=ANYMAL_C_CFG)
    ```
    The reference count is 3: one for the `robot` variable and two for the
    callbacks we launched on separate threads.
    
    Now, if we try to delete the object by doing `del robot`, the reference
    count becomes 2. These correspond to the callbacks that were launched.
    However, the destructor `__del__()` isn't called yet because it is only
    done when all the reference counts are 0.
    
    As a fix, this MR uses a proxy weak reference to "self" when passing it
    to underlying callbacks or other classes. This does not increment the
    reference count for the object; thereby, when the main object is
    deleted, all ts proxy references become invalid as well.
    
    ## Type of change
    
    - Bug fix (non-breaking change which fixes an issue)
    
    ## Checklist
    
    - [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
    `./orbit.sh --format`
    - [x] I have made corresponding changes to the documentation
    - [x] My changes generate no new warnings
    - [x] I have added tests that prove my fix is effective or that my
    feature works
    - [x] I have updated the changelog and the corresponding version in the
    extension's `config/extension.toml` file
    8ba18efa
Name
Last commit
Last update
.github Loading commit data...
.vscode Loading commit data...
docker Loading commit data...
docs Loading commit data...
source Loading commit data...
.dockerignore Loading commit data...
.flake8 Loading commit data...
.gitignore Loading commit data...
.pre-commit-config.yaml Loading commit data...
LICENSE Loading commit data...
README.md Loading commit data...
VERSION Loading commit data...
orbit.sh Loading commit data...
pyproject.toml Loading commit data...