Problems with CList MFC

EugeneTrukhin
EugeneTrukhin used Ask the Experts™
on
Hi,
I have a problem with a linked list of pointers declared in the CMyAppDoc class: CList <RegularState *> stateList.
I want to add a pointer to myClass object  to the list after the user clicks OK in the dialog box. Here goes the part of the code.

void CMyAppDoc::OnAppModifyState()
{
     ModifyState modifyStateDlg;
     if (modifyStateDlg.DoModal() == IDOK)
     {     //get the string:
          LPCTSTR temp = modifyStateDlg.stateLabel;
                //creating a pointer
          RegularState *tempSt = new RegularState();
                //store the string
          tempSt->setStateLabel(temp);
                //add to the list
          stateList.AddTail(tempSt);
     
     }
}
When the program is executed, after clicking OK, the program crashes.

If you have some solution to this problem, please answer me, I will be very pleased.
Thank you for your efforts.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
when the modifyStateDlg goes out of scope, so will stateLabel and won't that invalidate temp?  If temp is being stored/used after this occurs -> crash.

 does setStateLabel make a copy of temp?

Author

Commented:
Yeah, setStateLabel copies temp into LPCTSTR variable of the class RegularState.
No, temp is not invalid after the modifyStateDlg goes out of scope. I checked that by displaying the pop-up window with the value of temp. The string of characters is stored in the temp. I tested it...I guess the problem is in the linked list but i dont have enough knowledge of MFC's Clist linked list functionality.
If I remove this line :
 stateList.AddTail(tempSt);

the program doesn't crash.
Did anybody have any problems with CList linked lists?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Do you have a declaration like this?

 CList<RegularState*, RegularState*> stateList;

Author

Commented:
No, i didn't..I had it like this:
CList<RegularState*> stateList;
But then after your comment I changed to
CList<RegularState*, RegularState*> stateList;
but it still crashes..
By the way i;m using VC++ .Net. But i guess it should not be a problem.

Commented:
This is not really a solution, but you should try using the debugger.  At the very least you'll be able to find the root of your problem.  If you don't know how to use a debugger, let me know and I can give you some pointers.

Brian

Author

Commented:
Surely, i used a debugger. The problem was not there. When i changed the program so that the handler runs from the CView but not CDoc as before. Then, the data is now stored through the CView in the CDoc. And now it works. I guess that was a problem in a proper usage of Document/View architecture. Im new to the MFC; therefore, i got this error.

Author

Commented:
by the way bree2474, I;m also from Concordia university, Montreal, Computer Science but i'm doing my undergraduate degree:)

Commented:
Hey cool.  Are you using MFC for a class, or just on your own?  I just picked it up on my own.  BTW, my e-mail is bknoep@videotron.ca on case you want some advice on what profs are good, and what courses to take or stay away from.

Brian

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial