advertisement

Article:
  Memory Management in Objective-C
Subject:   Object Semantics
Date:   2001-07-31 07:29:27
From:   canyonrat
Response to: (Q) interchangeable pointers and objects

I think it's dangerous to generalize here. You just have to accept that ObjC is an OO language that uses pointer semantics and move on. Object Pascal also uses pointer semantics but Java uses reference semantics and C++ uses copy semantics.


All these languages are dealing with the same question: "How do I return an object from a method that will remain valid when the method goes out of scope?"


C++ takes the most straightforward approach and copies the object. There is no behind the scenes conversion from myType to myType*. This puts the burden on the programmer of defining a copy constructor for all but the most trivial classes. It also can really bog a program down in copying.


Java attempts to solve the problem with a universal garbage collector. At least in 1.2 it didn't really work and for a serious program the programmer had to manage memory "by hand." It may be fixed now.


ObjC does return a myType* and asks the programmer to autorelease it or to make it with a convenience constructor that shifts memory management duties to the class object.


1 to 1 of 1
  1. Object Semantics
    2001-07-31 10:42:35  psheldon [View]

    • Where is the folklore
      2001-08-01 20:54:55  canyonrat [View]

      • Where is the folklore
        2001-08-02 14:21:24  psheldon [View]

        • cocoa-dev with browser-nomail option
          2001-08-02 21:24:14  psheldon [View]

        • Where is the folklore
          2001-08-02 20:46:40  canyonrat [View]

          • thanks, checked omnigroup (nomail?)
            2001-08-02 21:40:48  psheldon [View]

            • thanks, checked omnigroup (nomail?)
              2001-08-03 07:29:38  canyonrat [View]

1 to 1 of 1