Skip to main content
Senior
February 27, 2025
Solved

Passing references from screen to model

  • February 27, 2025
  • 2 replies
  • 855 views

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?

Best answer by Flemming Gram CHRISTENSEN

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.

2 replies

GaetanGodart
Technical Moderator
February 28, 2025

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,

PriyankAuthor
Senior
February 28, 2025

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.

PriyankAuthor
Senior
February 28, 2025

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

Flemming Gram CHRISTENSEN
ST Employee
March 6, 2025

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.