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. Getting around vanishing pointers

Getting around vanishing pointers

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
5 Posts 4 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.
  • B Offline
    B Offline
    brdavid
    wrote on last edited by
    #1

    Take for example the following code CItemInfo *lp = new CItemInfo() lp->SetItemText("String"); CTreeItem *pParent = pList->InsertRootItem(lp); delete lp; the following is for inserting data into a TreeList control. the problem here is that the command delete lp will delete the information stored in the treelist. If I change lp from heap to stack, I get the same result. If I remove delete lp then the information stays. Is there a way I can prevent this kind of occurance? I would like to clean up my pointers if possible but that seems to corrupt existing data. Thanks!

    B M C B 4 Replies Last reply
    0
    • B brdavid

      Take for example the following code CItemInfo *lp = new CItemInfo() lp->SetItemText("String"); CTreeItem *pParent = pList->InsertRootItem(lp); delete lp; the following is for inserting data into a TreeList control. the problem here is that the command delete lp will delete the information stored in the treelist. If I change lp from heap to stack, I get the same result. If I remove delete lp then the information stays. Is there a way I can prevent this kind of occurance? I would like to clean up my pointers if possible but that seems to corrupt existing data. Thanks!

      B Offline
      B Offline
      bneacetp
      wrote on last edited by
      #2

      Is this code within a class's member function? Happy Programming and God Bless! "Your coding practices might be buggy, but your code is always right." Internet::WWW::CodeProject::bneacetp

      1 Reply Last reply
      0
      • B brdavid

        Take for example the following code CItemInfo *lp = new CItemInfo() lp->SetItemText("String"); CTreeItem *pParent = pList->InsertRootItem(lp); delete lp; the following is for inserting data into a TreeList control. the problem here is that the command delete lp will delete the information stored in the treelist. If I change lp from heap to stack, I get the same result. If I remove delete lp then the information stays. Is there a way I can prevent this kind of occurance? I would like to clean up my pointers if possible but that seems to corrupt existing data. Thanks!

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        See if your TreeList can associate an LPARAM with each item, like regular trees and lists do. If so, put the pointer in each item's LPARAM. Then when the items are removed from the control (TVN_DELETEITEM for trees, LVN_DELETEITEM for lists) delete the pointer then. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- I even hear the Windows "OMG I booted up fine" sound.   -- Paul Watson diagnosing hardware problems.

        1 Reply Last reply
        0
        • B brdavid

          Take for example the following code CItemInfo *lp = new CItemInfo() lp->SetItemText("String"); CTreeItem *pParent = pList->InsertRootItem(lp); delete lp; the following is for inserting data into a TreeList control. the problem here is that the command delete lp will delete the information stored in the treelist. If I change lp from heap to stack, I get the same result. If I remove delete lp then the information stays. Is there a way I can prevent this kind of occurance? I would like to clean up my pointers if possible but that seems to corrupt existing data. Thanks!

          C Offline
          C Offline
          CaesarCZ
          wrote on last edited by
          #4

          I guess there is not. The CTreeItem only stores the pointers you give him, not the actual data. So if you delete the data, the pointer the CTreeItem has is useless. Why don't you just delete them once you stop using the CTreeItem? (I have never worked with MFC etc, but this is a common practice)

          1 Reply Last reply
          0
          • B brdavid

            Take for example the following code CItemInfo *lp = new CItemInfo() lp->SetItemText("String"); CTreeItem *pParent = pList->InsertRootItem(lp); delete lp; the following is for inserting data into a TreeList control. the problem here is that the command delete lp will delete the information stored in the treelist. If I change lp from heap to stack, I get the same result. If I remove delete lp then the information stays. Is there a way I can prevent this kind of occurance? I would like to clean up my pointers if possible but that seems to corrupt existing data. Thanks!

            B Offline
            B Offline
            brdavid
            wrote on last edited by
            #5

            Thanks for the tips, guys. I'll try looking in that direction! :cool:

            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