![]() ![]() What we need to do (more or less) is to traverse this cross-link back to its source, to get the real reference to the data. If we modify s.x‘s reference we’re just killing the so-called cross-link of the shared-array. Don’t forget that s.x is merely a reference copy of the original property data. So we can simply get this pointer in our Mex function and use it to modify the original property value. We can use another trick here, which is a corollary to the COW mechanism: when we pass s.x into a function, it is not a data copy that is being passed ( by value), but rather its pointer ( by reference). ![]() So the trick is to get the reference address (pointer) of s.x and s.y. Once we have the reference to the private data, we can modify the data in-place using a bit of Mex. However, we can use this information to our advantage by retrieving the field references (pointers) before COW has a chance to ruin them. This is done automatically and we do not have any control over it. It is only when trying to modify the struct fields that COW kicks in and a real copy is made. This basically means that when our struct is created, the field values actually hold references (pointers) to the original object properties. The core idea behind the solution is Matlab’s Copy-on-Write mechanism (COW). mxSetProperty is a dead-end for similar reasons. Mex’s standard mxGetProperty cannot be used on the original object, because mxGetProperty returns a copy of the property (not the original reference – probably to prevent exactly what I’m describing here…), and in any case it can’t access private properties. Even if our original object is a handle class, the struct would still be a shallow copy and not a real reference to the object data. ![]() Unfortunately, this does not affect the original class properties, since our struct is merely a copy of the original. The natural attempt would now be to update the struct’s fields with new values. We can now read all the private internal properties of the object. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |