home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / mac / programm / 13895 < prev    next >
Encoding:
Text File  |  1992-08-12  |  2.7 KB  |  59 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!destroyer!gumby!kzoo!k044477
  3. From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
  4. Subject: Re: m e m o r y m a n a g e m e n t
  5. Message-ID: <1992Aug12.201804.14518@hobbes.kzoo.edu>
  6. Organization: Kalamazoo College
  7. References: <1992Aug12.192637.13847@adobe.com>
  8. Date: Wed, 12 Aug 1992 20:18:04 GMT
  9. Lines: 48
  10.  
  11. jryan@adobe.com (Jim Ryan) writes:
  12. >I'm a tad cloudy on when/where the "Dispos(e)" routines need to be used
  13. >to clean up memory.  
  14.  
  15. Most of the questions like "do I need to dispose of a FooStruct's
  16. SubFooStruct manually, or will DisposeFooStruct() do it for me?" are
  17. answered in IM if you read carefully.  
  18.  
  19. In general, if NewFooStruct() gives you the option of passing NULL and
  20. letting the toolbox allocate it...it's exactly the same as if you call
  21. NewPtr() to get the memory and pass that pointer in.  Allocating
  22. nonrelocatable stuff is fine if either (1) it's done early in your app's
  23. lifetime, _or_ (2) it doesn't hang around for very long--modal dialogs,
  24. for example, don't last too long.
  25.  
  26. >2.  Say I have a C function that has some local variables like:
  27. >Handle theHandle;
  28. >I guess what I'm not entirely sure of is, when does one need to dispose
  29. >of such variables, and when will they die with the termination of the 
  30. >function?
  31.  
  32. The space occupied by the _variables_ is on the stack, and will be
  33. reclaimed when you exit the function.  That's only four bytes per handle
  34. or pointer, however, and you're probably concerned about the space which
  35. the variables _refer_to_.  That won't be recovered until you call
  36. DisposXxx()...and if a function stores the result of NewHandle() in a
  37. local var and exits without cleaning up, you have no way of knowing what
  38. to pass to DisposHandle().
  39.  
  40. >3.  Resources... when do you dispose of them, release them, etc.
  41.                               ^^^^^^^
  42.  
  43. Never!  Unless you like a corrupted resource map.  Always call
  44. ReleaseResource() on resources and DisposHandle() on non-resources.
  45. Release a resource when you're done with it, make it purgeable if you're
  46. not completely done but you don't need it right away.
  47.  
  48. >4.     Any other memory issues I've missed and/or should be aware of?
  49.  
  50. It's a craft, not a science...it's pretty easy actually, once you get
  51. into it.  Bo3b Johnson's "Leaks" dcmd is _invaluable_ for finding memory
  52. that you're not disposing of.  It will save you many, many, many hours,
  53. and I'm not kidding about this.
  54. -- 
  55.  Jamie McCarthy      Internet: k044477@kzoo.edu      AppleLink: j.mccarthy
  56.  "I honestly don't have the time to bicker with you and explain
  57.   everything to you.  Nor do I have the time to devote to a proper
  58.   debate about the Holocaust."         - Dan "Maynard" Gannon, 20 July 1992
  59.