CStringList or CStringArray

Posted on 1997-07-23
Last Modified: 2013-11-20
This should be a simple question using VC++ 4.2 but something isn't working too well. I have to transfer data from a listbox before it closes, to use it in the calling class. I've jumped in at OnOK and have attempted to put everything into a CStringList. This was done in many ways, but none worked.

The one that seemed to be fail proof was to go to the calling class and insert the string in the CStringList there, within that class. It inserts it fine (i.e. AddTail). However, once the calling Dialog closes I find the CStringList is empty, though the insertion had no problems. Remember, the dialog is in another class, so the fact that it closes should not affect the different class the CStringList is in. Though this seems to make sense it certainly doesn't work.

I've used a CStringList as I don't know how many elements will be in the list. Maybe a CStringArray is a better solution. Or maybe you have another suggestion.

Thanks for your much needed wisdom!
Question by:RJV
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Accepted Solution

gelbert earned 100 total points
ID: 1302755
I think your problem is that CStringList stores pointer to memory where data is located. So when you close dialog( and destroy class I think which allocated memory for strings of listbox), you destroy memory used to store strings from listbox. As result your CStringList points to undefined memory( You can expect to fuind there anything).
  Whatever you are using to store strings make sure that you allocated memory first, copy strinds there and save pointer to this memory instead of memory of dialog.

Author Comment

ID: 1302756
Hi Gelbert,

This is the part that has been unclear in the MFC documentation. It doesn't state clearly that pointers are stored. From what you indicate the best bet is a CStringArray in the destination class.

What is curious is that though the dialog is destroyed, when I access the CStringList later I get a NULL POSITION. I should get a pointer to junk, seeing as the CString was destroyed. That is what doesn't make sense.


Expert Comment

ID: 1302757
Answer is somwhere else. In OnOK() function dialog still remains in the memory and is destroyed later. So in OnOk() all dialog members are still valid and still accessible in the memory.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1302758
True, OnOK does leave all valid, which is why I implemented it. The problem is more complex.

Essentially, only the calling function has access to any of the memory in the dialog. If you go to any other function in the class that loaded the dialog, you will not have access to the information in the dialog's class. Just test it to see this.

The inverse is also true, though slightly different. If you go from the dialog class to the calling class before the dialog closes (such as from OnOK), the access to the memory variables in the calling class is not valid. Put anything you wish in them (public or restricted, makes no difference) then finish up with the dialog and see what is in those variables (in the calling class, not the dialog's class). They will be empty.

The problem seems to be associated to MFC's locking classes to views, particularly if the dialog is modal. It isn't easy to change anything in the calling dialog or property sheet (my case) from the called dialog.

I'd run across this type of problem in the past, though not as intensively as now. After nearly a day of testing and using the likes of 'new' and 'malloc' I was able to find out exactly what was happening and then get things to work.

As to CStringList, Gelbert, I'm still not certain about your comment. Indeed, the documentation states that it stores pointers. However, I've seen many examples that transfer text to lists and immediately change or destroy the CString or variable with the original text in it. So the matter is indeed confusing.

Can anyone shed more light on this matter? I'm sure it will help everyone.


Expert Comment

ID: 1302759
Hello RJV,
I do programming with dialogs for a long time and I do not exactly know, why you have these problems. I can access anything I want from dialog and anytime I want. Your problem can be with DDX - do you use it?. If you use it I hope you know about UpdateData() function to update your controls.
Send me the part of code ( where you have problems and I will help you.
Kind regards,

Expert Comment

ID: 1302760
Hello RJV,

  If memory which pointed by your CStringList is freed you can find there
anything: junk, NULL, valid data. It depends who managed to use it between freeing and using CStringList.

Author Comment

ID: 1302761
In answer to both, gelbert the CString supplying CStringList is indeed freed after you close the dialog. Remember, though, that you can access any variable in a dialog even after you close it. This is a standard proceedure. So the data there should be valid. On the other hand, as I indicated, you use one CString for all data. As such, it isn't freed but it changes constantly. Within this context nothing much would be valid for a CStringList.

As to dialogs, yes I use DDXs extensively, Jasiek. I've also done a lot of work with dialogs, from modal to modeless to propertysheets and pages and to dialog bars. That's why this problem caught me completely by surprise. Give it a try.

Include a variable in one modal dialog (property page in my case) and then call another modal dialog. From that dialog, go to OnOK. From there call a function in the first dialog (i.e. via a 'dialog pDlg'). In that function put something in that variable. Then close the topmost dialog (last one called) and take a look at the variable. If you're like me I think you'll be surprised, particularly from what you said. It should be NULL.


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 55
Detect CR LF to each line 12 176
no14 challenge 14 74
zeroMAx challenge 20 127
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question