Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!usc!apple!portal!atari!kbad From: kbad@atari.UUCP (Ken Badertscher) Newsgroups: comp.sys.atari.st.tech Subject: Re: Malloc() and desk accessories Message-ID: <2261@atari.UUCP> Date: 28 Aug 90 19:28:55 GMT References: Organization: Atari Corp., Sunnyvale, CA Lines: 42 As long as you don't go back into an event wait, you can Malloc all day long (Mfreeing what you Malloc when you're done with it, of course). The only time DA's have Malloc headaches if they try to Malloc memory outside their startup sequence is if they give the parent application a chance to terminate out from under them. The problem arises when an application calls appl_exit(). The AES does not give DA's enough time to clean up after themselves before the parent application's resources are released. It is possible for a DA to not get an AC_CLOSE message until you're already back at the desktop. Note that this also causes problems with DA's that open virtual workstations, since the VDI Mallocs memory for workstation information. A sequence of events like the following could cause problems: Run an application. Open a DA that opens a workstation or Mallocs some memory. Quit the application without closing the DA. At this point, the application calls appl_exit(), which returns before giving all DA's a chance to clean up. The application then terminates, and GEMDOS frees all the memory that was allocated while the program was running. Eventually, the AES will get around to telling the DA, "Oh, by the way, shut yourself down now." If the DA then tries to access any of the memory it had Malloced, it would be messing with memory that had already been freed. If this memory had since been reallocated, the DA would be changing someone else's memory. If it happened that the memory had been reallocated to someone else _at the same starting address_, it would be disastrous for the DA to Mfree that block. The solution is simple. DA's must only Malloc memory in an atomic (from the AES's point of view) operation, as Steve outlined. If a DA has more dynamic memory needs, it can allocate a heap for itself at startup, BEFORE IT MAKES ANY AES CALLS. The DA can then use it's own memory management within this heap. -- ||| Ken Badertscher (ames!atari!kbad) ||| Atari R&D System Software Engine / | \ #include