Infinite recursion when saving a custom object

5 Ansichten (letzte 30 Tage)
Claudio Vergari
Claudio Vergari am 3 Dez. 2021
Kommentiert: Claudio Vergari am 3 Dez. 2021
I have a Parent and a Child object, and I need to store a reference to the Parent in the Child. It works, but I cannot seem to save the object because Matlab goes into an infinite loop.
Here is a working example:
classdef Parent
properties
Child = [];
Color = 'Red';
end
methods
function ret = get.Child(this)
% Returns a Child instance, with this Box as a parent
ret = Child(this);
end
end
end
classdef Child
properties
Parent = [];
end
methods
function this = Child(Parent)
% Initialize by saving a reference to the Parent
if exist('Parent' , 'var')
this.Parent = Parent;
end
end
function ParentColor(this)
% Display the parent's color
disp(['My Parent is ' this.Parent.Color])
end
end
end
% This works
P = Parent();
P.Child.ParentColor
% This goes into an infinite loop
save('D:\matlab_recursion\Box.mat', 'P')
When saving, Matlab starts looping between get.Child and and Child's initialization.
Any ideas on why this happens, and how I could solve it?
In real life, it would not be practical to just store the parent's Color property in the child because I need to access A LOT of properties and methods of the parent...
  2 Kommentare
Matt J
Matt J am 3 Dez. 2021
Bearbeitet: Matt J am 3 Dez. 2021
I need to access A LOT of properties and methods of the parent...
That sounds like it might be better to make the child a sub-class of the parent.
Claudio Vergari
Claudio Vergari am 3 Dez. 2021
It could, I'll think about that. My child class is a "calculator" that computes stuff on the data in the parent class. The methods of my child could be methods of the parent, but I separate them to have a more logical organization. If I sub-classed the parent, I would have to make a copy of all the data in the parent when I want to compute stuff, and I don't want that.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 3 Dez. 2021
Bearbeitet: Matt J am 3 Dez. 2021
It happens because save() does know that the parent and child properties are supposed to be references to one another. It tries to de-reference all the property values when saving.
Here is a remedy:
classdef Parent<handle %<----make this a handle class
properties
Child = [];
Color = 'Red';
end
methods
function obj = Parent
obj.Child=Child(obj); %<---create the child in the constructor
end
end
end
classdef Child
properties
Parent = [];
end
methods
function obj = Child(p)
% Initialize by saving a reference to the Parent
if nargin
obj.Parent = p;
end
end
function ParentColor(obj)
% Display the parent's color
disp(['My Parent is ' obj.Parent.Color])
end
end
end
  3 Kommentare
Matt J
Matt J am 3 Dez. 2021
Bearbeitet: Matt J am 3 Dez. 2021
Matlab has a copy-and-write system, meaning that if you create a structure like this in the Matlab workspace,
data=rand(512,512,512); % 1 GB of RAM
S.a=data;
S.b=data;
Matlab knows that S.a and S.b refer to the same data and so additional RAM is not allocated when the struct is created. (If you change even 1 element in either S.a or S.b, an additional 1-2 GB will be allocated). However, when you save S to a .mat file, the save() command is not smart enough to see that a and b are the same data. It just saves independent deep copies of both variables in the file, resulting in a file size of about 2 GB.
Claudio Vergari
Claudio Vergari am 3 Dez. 2021
Thank you so much for taking the time to explain. I think I undestand: when I assign the property at the parent's initialization, Matlab immediately makes a copy of the child object because stuff happens in the child's initialization. Whereas if I leave it for later, Matlab tries to "get" the property, which instead of referencing the Parent without copying it, this time it has to recompute the parent, which will reinitialize the child, etc..

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by