![]() ![]() This raises a concern that any internal Matlab object that has properties which are handle objects might incur hard-to-trace bugs due to this behavior. Certainly programmers who come from other programming languages would not expect this behavior. I venture a guess that if a poll was made among MathWorker developers, the majority (if not vast majority) of them are not aware of this fine detail. In recent releases, Matlab increasingly relies on MCOS (Matlab’s latest OOP incarnation) for its internal codebase. But confusing? Counter-intuitive? Unexpected? – you bet! However, this does not happen in handle classes, so modifying Internal‘s property in one instance of External also affects the other instances. The reason is that Matlab’s built-in COW ( copy-on-write) mechanism ensures that we get a new copy of a value class whenever one of its properties is modified. This does not affect properties that are initialized to primitive (non-object) data, nor to value classes (as opposed to handle classes). So basically, all instances of External share the same Internal object! This same handle reference (memory pointer, in a broad sense) is used for all subsequent creations of External class instances. In other words, when Matlab first loads the External class code into memory (typically upon its first instance creation), it assigns the handle reference to a new Internal instance to the class property. MATLAB does not reevaluate the expression each time you create a class instance. The answer lies in the fine print of the documentation:Įvaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. > Internal.getDefault % this proves that the default Internal value remains unchanged (as expected)īasically, this shows that setting e1.MyValue.Value=1 has also affected future class constructions, such that e2=External (which should have created a new handle object with a default property value of 0) now returns the modified value 1. > e1 = e2 % this proves that e1~=e2 (as expected) getDefault % this proves that the default Internal value remains unchanged (as expected) ans = > e1 = e2 % this proves that e1~=e2 (as expected) ans = 0 e2 = External % This returns a bad value of 1 (should be 0!) ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |