Re: Structs Question
- Posted by euman at bellsouth.net Jul 14, 2001
- 387 views
Pretty good lesson I think....> Euman euman at bellsouth.net > > > Never mind I figured it out. > > if I free(iParent) before I send this message > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > there wont be any text for the root item. > > Yep, that's the problem alright. Same with Child1 and child2. > > > Now how in hek is this? > > > > I poked the text into the structure then free'd the allocated_string > before > > I sent the message and it doesnt work and when I move the free() > > after the message......hmmm Im at a loss. (beats me) but it works now. > > Well, actually you didn't poke the TEXT into the structure. All you do is > poke the ADDRESS of the text into the structure. > > > iParent = allocate_string("Parent Item") > > The allocate_string() reserves a place in RAM for you text, copies the text > in the sequence to that spot, and returns to your program the location > (address) reserved for you. > > > poke4( tvstruct + TVINSERTSTRUCTITEM_pszText,iParent) > > This poke4() copies the address returned to the struct. An address is > 4-bytes long and that's what the poke4() function does - copies 4-byte to an > RAM location. > > > free(iParent) > > This tells Windows that you no longer need the RAM location that was > reserved for your text and gives it back to Windows. After this, your text > may or may not be in the RAM still, it all depends on how Windows uses that > RAM again after you gave it back. > > You cannot use or expect to use RAM that you have given back to Windows. > > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > However, you then send the message to Windows to insert an item. The Windows > code that does this doesn't know you gave back the RAM and will attempt to > use the address you poked into the structure. But by this time, the spot > could have been used by anything else and destroyed your text. > > If you notice, in win32lib, we always release_mem() after sending messages. > > ----- > Cheers, > Derek.