Crash after reading a linked list CString
-
hi, i've made a linked list that contains info that i want to read in another function. Wenn i make a new record and insert it into the linked list my programm crashes the next time it reads the list. it can read everything except for the new record. i think it has something to do with the insertion of a wrong pointer. here's the code for inserting the new record:
Leverancier->LeverancierID = GetNewLeverancierID(); NewLeverancier = new CLeverancierStruct; NewLeverancier = Leverancier; m_LeveranciersLijst.AddTail(NewLeverancier);
the "Leverancier" variable contains the info that has to be inserted. The next time i try to read the new record the programm just crashes on the point of retreving a CString it's value(for the struct). []D [] []D [] -
hi, i've made a linked list that contains info that i want to read in another function. Wenn i make a new record and insert it into the linked list my programm crashes the next time it reads the list. it can read everything except for the new record. i think it has something to do with the insertion of a wrong pointer. here's the code for inserting the new record:
Leverancier->LeverancierID = GetNewLeverancierID(); NewLeverancier = new CLeverancierStruct; NewLeverancier = Leverancier; m_LeveranciersLijst.AddTail(NewLeverancier);
the "Leverancier" variable contains the info that has to be inserted. The next time i try to read the new record the programm just crashes on the point of retreving a CString it's value(for the struct). []D [] []D []The problem is most likely that
NewLeverancier = Leverancier;
does not copy the object pointed to by
Leverancier
into the object pointed to byNewLeverancier
: instead, it just reassignsNewLeverancier
to point to the previous object (and creates a memory leak BTW). What you should do is define a copy constructor for the classLeverancier
andNewLeverancier
belong to and then createNewLeverancier
like this:NewLeverancier = new CLeverancierStruct(Leverancier);
Also, I strongly recommend you that you use some standard container like
std::list
instead of your own handcrafted container, it'll save you a lot of headaches. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
hi, i've made a linked list that contains info that i want to read in another function. Wenn i make a new record and insert it into the linked list my programm crashes the next time it reads the list. it can read everything except for the new record. i think it has something to do with the insertion of a wrong pointer. here's the code for inserting the new record:
Leverancier->LeverancierID = GetNewLeverancierID(); NewLeverancier = new CLeverancierStruct; NewLeverancier = Leverancier; m_LeveranciersLijst.AddTail(NewLeverancier);
the "Leverancier" variable contains the info that has to be inserted. The next time i try to read the new record the programm just crashes on the point of retreving a CString it's value(for the struct). []D [] []D []hi, I think the problem is the assignment operator. If you have not coded an assignment operator for your CLeverancierStruct, the assignment does a shallow copy which will not copy the CString value(s). Thus you will copy the pointer to a CString object, which doesnot know that it is used twice, producing a memory access failure or something like that. Remember structs are classes which default to public for all members. Have a nice day, and post me if this is the reason. G. Steudtel :)
-
hi, I think the problem is the assignment operator. If you have not coded an assignment operator for your CLeverancierStruct, the assignment does a shallow copy which will not copy the CString value(s). Thus you will copy the pointer to a CString object, which doesnot know that it is used twice, producing a memory access failure or something like that. Remember structs are classes which default to public for all members. Have a nice day, and post me if this is the reason. G. Steudtel :)