Link to home
Start Free TrialLog in
Avatar of EugeneTrukhin
EugeneTrukhin

asked on

Problems with CList MFC

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.
ASKER CERTIFIED SOLUTION
Avatar of danny_pav
danny_pav

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 does setStateLabel make a copy of temp?
Avatar of EugeneTrukhin
EugeneTrukhin

ASKER

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?
Do you have a declaration like this?

 CList<RegularState*, RegularState*> stateList;

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.
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
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.
by the way bree2474, I;m also from Concordia university, Montreal, Computer Science but i'm doing my undergraduate degree:)
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