2025-02-27 10:51 AM
Hi, I was curious on the operation of the tearDownScreen function in regards to local variables created in the screen views hpp file. If I create a Struct A in screenview, and pass it by reference to the presenter then to the model, is this recommended against since tearDownScreen would get rid of the struct A? Is it recommended to dynamically create the Struct A in the screen view then delete it in the tear down screen function after sending it to the model?
2025-02-28 7:40 AM
Hello @Priyank ,
If you can create your structure directly in Model.cpp, it is strongly recommended instead of creating it in a screen then passing it to the model.
Can you do that or are you blocked by something?
Regards,
2025-02-28 1:02 PM
Hi, the structures are all created in the model/a struct file. The situation is that the data is stored in the model. So, when I pass data (structs) to the screenviews, I pass them as const references and assign them to a local variable of the struct. So my screen view would have somehting like this.
In the hpp protected section
StructA var;
and in the screenview cpp would be
var = presenter->getStructA;
presenter would be
const StructA& getStructA() const {model->getStructA();};
and model would have a similar function.
2025-02-28 1:02 PM
So then when updating the struct, I am now modifying the local copying and sending that back as a reference
2025-03-04 8:25 AM
Hello @Priyank ,
If you include your model in your screen you would be able to use the structure so you could pass your structure just like you would any other variable.
Have you tried that? If not, why not?
Regards,
2025-03-04 8:27 AM
Hi, I have tried that and it works, I was asking because I wanted to know best practices for this situation in touchGFX, because I was not sure how destruction of the screens copy of the struct would impact sending it as a reference back to the model.
2025-03-06 4:03 AM
Hi,
A little background:
As you know there is always only one Screen class allocated. It is therefore not allowed to have pointers or references to variables in a Screen saved in the Model and keeping them after a transition.
The (relevant) code executed when changing screen is this:
Screen tearDownScreen() is called first, then Presenter deactivate(), then the destructors are executed.
After this the new Screen and Presenter pair is allocated. This will overwrite the memory of the previous Screen.
Model is never deallocated.
I think the best style is to have the value/object in Model, put a reference in the presenter/screen to that object.
Or, also fine, have a value/object in Model and Screen, and then save to the Model object in tearDownScreen.
It is fine to return a reference in your getter methods, as long as the Model takes a copy of the object.
Let me know, if you have other questions.