Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!labrea!Portia!Jessica!rick From: rick@Jessica.stanford.edu (Rick Wong) Newsgroups: comp.sys.mac.programmer Subject: Re: Object Pascal and memory management Message-ID: <532@Portia.Stanford.EDU> Date: 28 Feb 89 17:38:48 GMT References: <604330910.18716@minster.york.ac.uk> Sender: news@Portia.Stanford.EDU Reply-To: rick@Jessica.stanford.edu (Rick Wong) Organization: Stanford University Lines: 61 In article <604330910.18716@minster.york.ac.uk> alistair@minster.york.ac.uk writes: >Can anyone out there explain how Object Pascal objects are allocated >in memory, and the relationship to segementation? > You've come to the right place! > >New allocates some memory on the heap and thing is set as an indirect >pointer to it. What goes in that bit of memory: the code for Tthing >(i.e. for Tthing.p) and/or space for the instance variable i? What if >I call new for another Tthing object? Surely I don't need another copy >of the code for Tthing.p, but do I get one anyway? What does >dispose(thing) do? > Only the space for the object's instance variables are allocated. The code is loaded in from your application's CODE resources when needed. There is at most one copy of the code in memory at any time, regardless of the number of instances of Tthing. Dispose(thing) should dispose the object by calling DisposHandle on the object handle. Rather than call dispose, you should use the standard TObject interface and call thing.Free, which will give your object a chance to clean up after itself and free any other things it may have allocated. > >And then... I have had to segment my program because the compiler began >to complain about segments >32K, but what does that mean in the context >of objects? Can I unload a segment I have finished with by something >like UnloadSeg(@thing.p) - and what would it mean if I did? How would >that work in relation to a dispose(thing)? > UnloadSeg and dispose are completely unrelated. Freeing an object only releases the memory occupied by its instance variables (i.e., its handle). The code will remain in memory until you call UnloadSeg on it. Calling UnloadSeg in Object Pascal may be tricky. I've never tried it because MacApp does it for me automatically. The call you give for it (UnloadSeg(@thing.p)) doesn't make sense -- you have to pass it the address of a procedure or function. This is probably where you're running into trouble, since I suspect you may be inadvertently passing the address of some sort of method dispatching table. To be safe, you should declare a dummy normal Pascal procedure in each of your unloadable segments and use that to unload them, e.g.: {$S Segment1} procedure StupidDummyRoutineForUnloadingSegment1; begin end; . . . and in your main event loop: UnloadSeg(@StupidDummyRoutineForUnloadingSegment1); Hope this was helpful. Rick "No, it's Hair Pie" Wong Courseware Authoring Tools Project, Stanford University rick@jessica.stanford.edu