[RESOLVED] How to prevent deleting dynamic objects

Hi, i have SDI app with CFormView main window, one of main frame variables is CDialog based class object. When i call from main frame this dialog i create there some dynamic objects by new operator. Then i close this dialog and reopen all created dynamical objects dissapear. Why is this happend. I thought if this cdialog is a member of main frame, all changes i make in this dialog will stay unchanged until i close main "frame window" == "close program" Why allocated memmory is automaticaly deallocated after closing diloag?? I need in my program to open and reopen some settings dialogs and after reopen they have to be the same as they was last time. And lot of dialog controls need to be created dynamicaly. Please help.
[736 byte] By [zarraza] at [2007-11-20 11:51:35]
# 1 Re: [RESOLVED] How to prevent deleting dynamic objects
I don't know the specific classes you're using at all, but is it possible that the objects you created still exist, and it's merely the pointers to them which have been lost?
Lindley at 2007-11-11 4:02:39 >
# 2 Re: [RESOLVED] How to prevent deleting dynamic objects
In an SDI app, typically the CFormView reads its display data from the document. As such, any modification to this data must be persisted back to the document. Okay, now onto the dialog. What need to do is load the dialog's data on creation and save it back in OnOk(). To make this easier, just pass a pointer to the document when you create the dialog.

// Invoked by some view handler
CMyDialog dlg( AfxGetDocument( ) );
dlg.DoModal( );

From there you just need to connect up the data from the document to the controls.

You can do this the traditional way of using the 'Add variable' method and DDX.

If you don't know how to do this, post back.

P.S. There is another cleaner way to do this, but you have to understand the 'Add Variable' method first.
Arjay at 2007-11-11 4:03:39 >
# 3 Re: [RESOLVED] How to prevent deleting dynamic objects
In an SDI app, typically the CFormView reads its display data from the document

Does it mean that if i for example create button in dialog class:

CMyButton* pDelBut;
pDelBut = new CMyButton();
CRect delButRect(130, 150, 190, 170);
pDelBut->Create("Usuń", WS_VISIBLE|WS_DISABLED, delButRect, this, m_iDelBut_id++);
m_vDelBut.push_back(pDelBut);

the m_vDelBut pointers vector has to be document member (not the Dialog class member) if i want to have acces to this button while app is running ??
zarraza at 2007-11-11 4:04:37 >
# 4 Re: [RESOLVED] How to prevent deleting dynamic objects
No. I was referring to staticly created controls (i.e. created in a dialog or formview template).

Do you need to create your controls dynamically? I mean, do you have a variable # or type of controls at runtime? If so, it is much more difficult to wire them up to the data because you can't just use the default DDX mechanism to let MFC handle the data chores (I guess you can make dynamic DDX entries yourself but this is pretty advanced stuff).

Can you post back if dynamic controls are a necessity?
Arjay at 2007-11-11 4:05:34 >
# 5 Re: [RESOLVED] How to prevent deleting dynamic objects
Dynamic controlls are necessary, because user decide about their number on runtime. I need to remember all controls if user changes something and later reopen this dialog its structure has to be loaded from for example vector of pointers to dynamic created controls. But i don't know why after closing dialog alocated memory (by new operator) i cleaned. If CFormView exist while app is running and dialog is view member i assume that all varables values (even dynamic) are remember all the time (to app close).
zarraza at 2007-11-11 4:06:44 >
# 6 Re: [RESOLVED] How to prevent deleting dynamic objects
Since you are using dynamic controls, I don't believe you can use the default DDX mechanism to wire up the data (although I assume it would be possible to create the DDX map entries - still there might not be much gain for the effort).

I would probably continue to track the controls in the vector as you are doing, but be sure to walk the memory and destroy and delete each control before closing the dialog. Everything that is associated with the display portion of the dialog should be cleaned up when the dialog goes out of scope (if you are using modal dialogs anyway).

You are still going to have to track the data associated with each dynamic control in the document.

Can you give me an example of the type of data you are storing (if you don't want to specify the exact data, make something up :gap:).

I am assuming that each 'number' the user selects contains a 'set' of data.

I think I might be able to offer an alternative based on the types of data.
Arjay at 2007-11-11 4:07:39 >
# 7 Re: [RESOLVED] How to prevent deleting dynamic objects
Are you creating a new CDialog based object each time, or reusing the same one?
GCDEF at 2007-11-11 4:08:42 >
# 8 Re: [RESOLVED] How to prevent deleting dynamic objects
At this time i just wan't to concentrate on reopen my dialog with the same number of controlls, placement, size and id's as before closing. And i don't know hot to do this. After app start i have main view with button, when clicked on it dialog appears and now user puts some controls and decide its placement. Next user close dialog (not main window), and reopens again and want to see everything what he done before (if he created 10 buttons he want them to be there at next opening). If i delete all dynamic data before closing dialog as you said, how i can reload all controlls structure, position, size etc...??
zarraza at 2007-11-11 4:09:45 >
# 9 Re: [RESOLVED] How to prevent deleting dynamic objects
Are you creating a new CDialog based object each time, or reusing the same one?

Dialog is member of MainView object (created by "add variable..." to class). I do this to make sure that dialog will be avaible until app is running (at least that was my idea), and not allow to clear dynamicall allocated memory.
zarraza at 2007-11-11 4:10:38 >
# 10 Re: [RESOLVED] How to prevent deleting dynamic objects
At this time i just wan't to concentrate on reopen my dialog with the same number of controlls, placement, size and id's as before closing. And i don't know hot to do this. After app start i have main view with button, when clicked on it dialog appears and now user puts some controls and decide its placement. Next user close dialog (not main window), and reopens again and want to see everything what he done before (if he created 10 buttons he want them to be there at next opening). If i delete all dynamic data before closing dialog as you said, how i can reload all controlls structure, position, size etc...??I believe I finally understand what this application does. It sounds like this app allows a user to build a UI by adding controls to a dialog. Different users are allowed to add a different number and type of controls. If this is not correct, then please describe the application in more detail.

Assuming the app's functionality is how I described it, what you need to track in a 'data' sense is the metadata for the controls associated with a user. For each user, you will need to track the type of control, the position of the control on the layout and the type of data that the controls store. I would envision that each user object has a list with 'control' entries in it - with each entry storing the position and type for each control. The document simply stores a list for each user.

When the 'layout' dialog opens, it just reads the data from the document for that user and starts building the controls one at a time. When the dialog is saved, it just saves the user data back to the document.

In terms of dialog control creation/destruction, when the 'layout' dialog is created with DoModal, the controls on the page are dynamically created and then destroyed when the dialog closes. So while the controls are always created/destroyed each time the dialog opens, the meta-data to describe the controls for the user is always persisted in the document.
Arjay at 2007-11-11 4:11:49 >
# 11 Re: [RESOLVED] How to prevent deleting dynamic objects
This app allows user to create very simple block diagram of some controller, lets say user just decide how many parts this block diagram will have. Those blocks are only buttons positionet to look like some kind of diagram. User opens this dialog to design this diagram at the beggining or to make some changes later (all at app run, app is not restarted and no saving anything to file). When he design this diagram he close it and open other dialog to test what he design. If something go wrong he reopenst dialog with his diagram and correct what he wants (user is always 1, no multiuser need).

I would envision that each user object has a list with 'control' entries in it - with each entry storing the position and type for each control.
I don't know if i understand what you mean. You wan't me to get size, position, id and type for each controll and store it in vector in document??

There is another thing that bother me, if i create some e.g. "integer" variable dynamically in this dialog and point to this variable by some pointer that is member of this dialog class, i can close and reopen this dialog and stilla have acces to this value. If i set this integer to 20, then close dialog and reopen it i still can read that value is 20. Why with Buttons and its pointers i can't do that??
zarraza at 2007-11-11 4:12:50 >
# 12 Re: [RESOLVED] How to prevent deleting dynamic objects
This app allows user to create very simple block diagram of some controller, lets say user just decide how many parts this block diagram will have. Those blocks are only buttons positionet to look like some kind of diagram. User opens this dialog to design this diagram at the beggining or to make some changes later (all at app run, app is not restarted and no saving anything to file). When he design this diagram he close it and open other dialog to test what he design. If something go wrong he reopenst dialog with his diagram and correct what he wants (user is always 1, no multiuser need). ??Okay, the design is similar to what I described.

I don't know if i understand what you mean. You wan't me to get size, position, id and type for each controll and store it in vector in document?If you only ever use buttons, then you don't need to store the type, but otherwise yes. The document will contain the list of users. Each user will contain the metadata list of controls (you don't need to use a vector because you don't need a container with contiguous memory - a list is more appropriate here).

There is another thing that bother me, if i create some e.g. "integer" variable dynamically in this dialog and point to this variable by some pointer that is member of this dialog class, i can close and reopen this dialog and stilla have acces to this value. If i set this integer to 20, then close dialog and reopen it i still can read that value is 20. Why with Buttons and its pointers i can't do that??Because you are creating the child controls of the dialog dynamically and destroying these controls when you close the dialog. If you were to store the pointers to the controls, they would be meaningless to the dialog when you re-opened it (because the control that used to belong to the pointer was destroyed the last time the dialog was closed - so now the pointer is invalid).

All you need to do is pass in the pointer to the User class into the dialog when you call domodal. In the OnInitDialog method, you will loop through the 'controls' list and create the controls. For each entry in the 'controls' list, you will call pBtn->Create and pass in the appropriate size and name info.
Arjay at 2007-11-11 4:13:48 >
# 13 Re: [RESOLVED] How to prevent deleting dynamic objects
Thank you Arjay, you helped me a lot. I create few vectors (i'm addicted to vectors :D ) one for size/placement , one for ID, one for style, and one for description. In OnInitDialog i "recreate" all controlls by passing to create function arguments from those vectors (vectors are members of CDialog derrived class because this dialog object exist all time while app is running, and in create() i can just use "this" as parent pointer). I overwrite DestroyWindow() function and inside i delete all dynamic controls i create. Pointers to those controls are also in vectors (as i said befoe i'm addicted). In DestroyWindow() vectors of pointers are cleared, and reloaded again in OnInitDialog() function while recreating all controls. I created few types of button (i mean each have to use different function on click event). To know button type i use diffrend range of id's ( e.g. 4001-4999 , 5001-5999). And it allow me to recognize type and create up to 999 buttons of each type.
zarraza at 2007-11-11 4:14:43 >
# 14 Re: [RESOLVED] How to prevent deleting dynamic objects
Great. Okay one minor suggestion. Now that you have it working with 3 vectors, replace that code with one vector that holds a class or structure (the class or structure would hold size/placement, ID, style, and description.

There isn't any reason to use 3 vectors when you can use 1 vector that contains a class (or structure) with all the necessary info for the control.

...and you were thinking I was going to suggest replacing the vector with a list. :D
Arjay at 2007-11-11 4:15:45 >
# 15 Re: [RESOLVED] How to prevent deleting dynamic objects
...and you were thinking I was going to suggest replacing the vector with a list. :D

No you don't guess :P. Actually i predicted your suggestion to replace those vectors with 1 structure. I thought about this too :D
zarraza at 2007-11-11 4:16:47 >