Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Crash after reading a linked list CString

Crash after reading a linked list CString

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Willem B
    wrote on last edited by
    #1

    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 []

    J G 2 Replies Last reply
    0
    • W Willem B

      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 []

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The problem is most likely that

      NewLeverancier = Leverancier;

      does not copy the object pointed to by Leverancier into the object pointed to by NewLeverancier: instead, it just reassigns NewLeverancier to point to the previous object (and creates a memory leak BTW). What you should do is define a copy constructor for the class Leverancier and NewLeverancier belong to and then create NewLeverancier 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

      1 Reply Last reply
      0
      • W Willem B

        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 []

        G Offline
        G Offline
        G Steudtel
        wrote on last edited by
        #3

        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 :)

        W 1 Reply Last reply
        0
        • G 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 :)

          W Offline
          W Offline
          Willem B
          wrote on last edited by
          #4

          i think this is the reason my programm crashes, but how can i prevent it? []D [] []D []

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups