Skip to content

TypeManager.static_pointer_array/TypeManager.dynamic_pointer_array are broken. #489

Open
@CookStar

Description

@CookStar

What pointer_attribute expects is CustomType not Array/BasePointer.

def __setitem__(self, index, value):
"""Set the value at the given index."""
self._make_attribute(index).__set__(self, value)

def fset(ptr, value):
"""Set the pointer attribute value."""
# Handle custom type
if not native_type:
# Set the pointer
ptr.set_pointer(value, offset)
# Make sure the value will not deallocate as long as it is
# part of this object
ptr._pointer_values[offset] = value
# Handle native type
else:
# Go down to "instance level"
instance_ptr = ptr.get_pointer(offset)
# Is there no space allocated?
if not instance_ptr:
# Allocate space for the value
instance_ptr = alloc(TYPE_SIZES[type_name.upper()])
# Add the pointer to the set, so there will be a reference
# until the instance gets deleted
ptr._allocated_pointers.add(instance_ptr)
# Set the pointer
ptr.set_pointer(instance_ptr, offset)
# Set the value
getattr(instance_ptr, 'set_' + type_name)(value)

And even if the Array inherits CustomType, the pointers set in the Array will be immediately discarded if not managed independently since the Array is dynamically created. (#490)

Code:

from memory.manager import CustomType
from memory.manager import TypeManager

type_manager = TypeManager()

class TestStaticPointerArray(CustomType, metaclass=type_manager):
    _size = 4
    static_vec_array = type_manager.static_pointer_array("Vector", 0, 1)

def test_static_pointer_array():
    test = TestStaticPointerArray()
    test.static_vec_array[0] = Vector(0.1, 0.1, 0.1)
    print(test.static_vec_array[0])

test_static_pointer_array()

Output:

[SP] Caught an Exception:
Traceback (most recent call last):
  File "../addons/source-python/packages/source-python/plugins/command.py", line 164, in load_plugin
    plugin = self.manager.load(plugin_name)
  File "../addons/source-python/packages/source-python/plugins/manager.py", line 209, in load
    plugin._load()
  File "../addons/source-python/packages/source-python/plugins/instance.py", line 74, in _load
    self.module = import_module(self.import_name)
  File "../addons/source-python/plugins/test/test.py", line 4703, in <module>
    test_static_pointer_array()
  File "../addons/source-python/plugins/test/test.py", line 4700, in test_static_pointer_array
    test.static_vec_array[0] = Vector(0.1, 0.1, 0.1)
  File "../addons/source-python/packages/source-python/memory/helpers.py", line 237, in __setitem__
    self._make_attribute(index).__set__(self, value)
  File "../addons/source-python/packages/source-python/memory/manager.py", line 505, in fset
    ptr._pointer_values[offset] = value

AttributeError: 'Array' object has no attribute '_pointer_values'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions