Skip to content

TypeManager.pointer_attribute are broken due to incompatibility with the C/C++ memory ownership model. #490

Open
@CookStar

Description

@CookStar
  1. The values given to pointer_attribute are managed by CustomType's _allocated_pointers/_pointer_values, but since CustomType may be generated from raw pointers, CustomType cannot have ownership of the value. This applies to CustomType generated by make_object(), nested CustomType, and Array generated by static_pointer_array/dynamic_pointer_array.
    (See also TypeManager.pointer_attribute cannot be safely used in nested CustomType. #480, TypeManager.static_pointer_array/TypeManager.dynamic_pointer_array are broken. #489).

  2. If the value set in the pointer_attribute is an object generated by Python/Boost.Python, the memory will eventually be double free if the memory is managed by the C++ side. This can be circumvented by the user, but this is incompatible with the actual Source.Python implementation. Furthermore, this means that when the user handles a pointer_attribute, it must be fully aware of how the memory is managed. While this may be feasible at the library level, it is impractical at the user level.

These two problems eventually lead to segmentation violation or double free or memory corruption, and these problems are very difficult or impossible to debug.

Proposal:

We should not assign the pointer of an object generated by Python/Boost.Python directly to other memory location, we should always make a copy of it and assign it, as in an earlier implementation (7690d96).

# Q: Why do we use copy instead of set_ptr?
# A: Maybe it's a shared pointer which means that other
# classes might also have the address of it. So, changing
# address in this particular class wouldn't affect the
# other classes.
value.copy(ptr, self[attr_type].__size__)

This means that _allocated_pointers/_pointer_values are no longer required. If the user needs to delete a copied object, they can always use CustomType._destructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions