cancel
Showing results for 
Search instead for 
Did you mean: 

Passing references from screen to model

Priyank
Associate III

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?

6 REPLIES 6
GaetanGodart
ST Employee

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,

Gaetan Godart
Software engineer at ST (TouchGFX)

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.

So then when updating the struct, I am now modifying the local copying and sending that back as a reference

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,

Gaetan Godart
Software engineer at ST (TouchGFX)

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.

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:

FlemmingGramCHRISTENSEN_0-1741262193957.png

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.