Would the implementation of the smart pointer idea be possible in Mathematica ?
Let's say I have an object "o" of the form Class[object] where object is an Association, and Class has the HoldFirst attribute.
It should be easy to add to a reference count, counting the number of times "o" is stored in other variables.
But how could we decrement this reference count automatically, if it's even possible ? The goal of the reference count would be to delete object when the reference count reaches 0.
Are certain functions called under the hood by Mathematica, when deleting a list for example, and we could capture this call with an UpValues definition on Class in order to decrement the reference count ?
Or is all the memory management done at C level and we don't have to access it ?
Temporaryattribute. – Oleksandr R. Feb 06 '15 at 10:31Temporaryattribute can be helpful especially in cases where a reference might otherwise be orphaned (I think this can happen withDownValueslocal toModulethough I do not recall the issues in any detail). – Daniel Lichtblau Feb 06 '15 at 16:57Temporarydoesn't prevent it, in that case. What's essential in that scenario is that the symbol is referenced by some other one, external to its scope, and that indeed, it has one ofDownValues,UpValuesorSubValuesdefined. In this scenario, it does not matter whether the referencing symbol is later removed or not. – Leonid Shifrin Feb 06 '15 at 18:25