home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 21288 < prev    next >
Encoding:
Text File  |  1993-01-12  |  2.2 KB  |  81 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!sunic!kth.se!hemul.nada.kth.se!d88-jwa
  3. From: d88-jwa@hemul.nada.kth.se (Jon WΣtte)
  4. Subject: Re: TCL: Bizzare Behavior Time
  5. Message-ID: <1993Jan12.151637.10811@kth.se>
  6. Sender: usenet@kth.se (Usenet)
  7. Nntp-Posting-Host: hemul.nada.kth.se
  8. Organization: Royal Institute of Technology, Stockholm, Sweden
  9. References: <1993Jan11.170231.17969@fsl.noaa.gov> <1993Jan11.220137.26434@kth.se> <96325@rphroy.ph.gmr.com>
  10. Date: Tue, 12 Jan 1993 15:16:37 GMT
  11. Lines: 68
  12.  
  13. In <96325@rphroy.ph.gmr.com> rrichter@link.ph.gmr.com (Roy Richter PH/32) writes:
  14.  
  15. >|> CFoo :: DoFoo ( )
  16. >|> {
  17. >|>     CBar * aBar = new CBar ;
  18. >|> 
  19. >|>     theBar = aBar ;
  20. >|> }
  21.  
  22. >Do I have to be this nutso careful when dealing with TCL Objects?
  23.  
  24. Yes, because TCL objects are all indirect (Handles)
  25. You never know when the address of the receiving variable
  26. is calculated. A possible scenario is that:
  27.  
  28.     theBar = new CBar ;
  29.  
  30.     1) The address of theBar is calculated
  31.     2) new is called, which moves memory, and happens to
  32.        move the calling object as well
  33.     3) the result is written into the address from 1) - BAD!
  34.  
  35. You can also lock the object:
  36.  
  37.  
  38. CFoo :: DoFoo ( )
  39. {
  40.     Boolean locked = Lock ( 1 ) ;
  41.  
  42.     TRY {
  43.  
  44.         theBar = new CBar ;
  45.  
  46.     } CATCH {
  47.  
  48.         Lock ( locked ) ;
  49.  
  50.     } ENDTRY ;
  51.  
  52.     Lock ( locked ) ;
  53. }
  54.  
  55. Note that you HAVE to encapsulate in TRY/ENDTRY because
  56. otherwise an exception will leave your object locked and
  57. corrupt the heap!
  58.  
  59. >And, does coding it like this protect me -- that is, is "theBar"
  60. >have a correct reference after the call to "new".  If so, when does that
  61.  
  62. Well, the stack can't move, so the temp variable will not
  63. move when memoryt moves, and a simple assignment will not
  64. move memory, so assignment to members is safe. Treat
  65. objects and ther members just as handles, and you'll be
  66. just fine!
  67.  
  68. >refence get corrected?  If
  69. >    theBar = new CBar;
  70. >is potentially bad, maybe that should be somewhere in the docs.
  71.  
  72.  
  73. It _IS_ in the docs (read about indirect objects) and also in
  74. the sample code for the class library.
  75.  
  76. -- 
  77.  -- Jon W{tte, h+@nada.kth.se, Mac Hacker Deluxe --
  78.  
  79.   Clearly, most humans are not rational beings; they are rationalizing beings.
  80.                      -- Mel Walker
  81.