You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeManager.pointer_attribute cannot be safely used in nested CustomType. #480
This is partially related to #472 and #474 though.
The values set in the TypeManager.pointer_attribute are managed by CustomType._allocated_pointers or CustomType._pointer_values, but if the original CustomType is managed by another object, they will be deallocated instantly. This means that pointer manipulation is not possible on nested CustomTypes.
My advice is we should stop using TypeManager.pointer_attribute. This feature has not been tested for a long time(#391) and should only be used in situations where you have full control over the memory of the values you are setting. (i.e. only non-native_type that you can manage yourself.) I found this out the hard way.
frommemory.managerimportCustomTypefrommemory.managerimportTypeManagermanager=TypeManager()
classTest(CustomType, metaclass=manager):
_size=12test2=manager.instance_attribute("Test2", 4)
classTest2(CustomType, metaclass=manager):
_size=8size=manager.pointer_attribute(Type.INT, 0)
other_test=manager.pointer_attribute("Test", 4)
test=Test()
test.test2.size=1# BROKEN!, the memory of size is already deallocated.test.test2.other_test=Test() # BROKEN!, the memory of Test is already deallocated.
test.test2.size=1# BROKEN!, the memory of size is already deallocated.
That's not entirely accurate. The memory allocated for size is not freed yet because its setter still hold test2. After the assignment is done, however, test2 goes out of scope along with everything it allocated. For example:
test=Test()
test2=test.test2test2.size=1# Here test2.size is still held by test2._allocated_pointers...deltest2# ...now size is being deallocated because test2 went out of scope.
So, the problem is not really pointer_attribute itself, but the fact that instance_attribute don't hold converted values:
If test2 was a pointer_attribute it would be held by test._pointer_values on assignment. Same goes for test2.other_test that is being held by test2._pointer_values which are also freed when test2 is no more. For instance, making Test.test2 cached would fix these issues:
That's not entirely accurate. The memory allocated for size is not freed yet because its setter still hold test2. After the assignment is done, however, test2 goes out of scope along with everything it allocated.
I know, that is exactly what I was explaining in the comment I wrote.
First, cached_property accepts getter and setter, not property, second, it is incompatible with data.ini, and third, that's just shuffling Python objects around to extend its lifetime. I understand objects lifetime, but others don't. That method cannot be used to prevent crashes.
The fundamental problem is that the _allocated_pointers and _pointer_values of CustomType are managing Python objects that should not be managed. Even if we use cached_property or some other method, there is no guarantee that the original Test2 can manage other objects in the first place (i.e. Test2 cannot contain Python objects since they can be created by make_object() at any time).
I wrote about the fundamental problem in #490, so I'll close this one.
First, cached_property accepts getter and setter, not property,
cached_property(property) is the equivalence of CachedProperty(property.__get__. property.__set__, property.__delete__, property.__doc__) (via CachedProperty.wrap_descriptor).
cached_property(property) is the equivalence of CachedProperty(property.__get__. property.__set__, property.__delete__, property.__doc__) (via CachedProperty.wrap_descriptor).
It seems I was testing with an old binary, my bad. But as noted above, cached_property is not a solution to this problem.
Activity
jordanbriere commentedon Jul 3, 2023
That's not entirely accurate. The memory allocated for
sizeis not freed yet because its setter still holdtest2. After the assignment is done, however,test2goes out of scope along with everything it allocated. For example:So, the problem is not really
pointer_attributeitself, but the fact thatinstance_attributedon't hold converted values:Source.Python/addons/source-python/packages/source-python/memory/manager.py
Lines 446 to 450 in f129713
If
test2was apointer_attributeit would be held bytest._pointer_valueson assignment. Same goes fortest2.other_testthat is being held bytest2._pointer_valueswhich are also freed whentest2is no more. For instance, makingTest.test2cached would fix these issues:Because now
test2lifetime would be dependant oftestitself.CookStar commentedon Aug 10, 2023
I know, that is exactly what I was explaining in the comment I wrote.
First, cached_property accepts getter and setter, not
property, second, it is incompatible with data.ini, and third, that's just shuffling Python objects around to extend its lifetime. I understand objects lifetime, but others don't. That method cannot be used to prevent crashes.The fundamental problem is that the _allocated_pointers and _pointer_values of CustomType are managing Python objects that should not be managed. Even if we use cached_property or some other method, there is no guarantee that the original Test2 can manage other objects in the first place (i.e. Test2 cannot contain Python objects since they can be created by make_object() at any time).
I wrote about the fundamental problem in #490, so I'll close this one.
jordanbriere commentedon Aug 10, 2023
cached_property(property)is the equivalence ofCachedProperty(property.__get__. property.__set__, property.__delete__, property.__doc__)(viaCachedProperty.wrap_descriptor).CookStar commentedon Aug 10, 2023
It seems I was testing with an old binary, my bad. But as noted above, cached_property is not a solution to this problem.