• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Memory Management (Part I)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Management (Part I)


  • Subject: Re: Memory Management (Part I)
  • From: Marcel Weiher <email@hidden>
  • Date: Fri, 1 Aug 2003 07:12:13 +0100

Where on earth did you get that from?!? Reference counting is a form of garbage collection. It isn't a perfect form of garbage collection because it can't handle cycles, but it still is a form of garbage collection. Retain/release, of course, is not fully automatic garbage collection, because it is at best semi-automatic.

The previous poster states that it is done to "work a little bit more like a garbage collected environment",
then I am saying: Don't think about garbage collection, just think in terms of autorelease pools. It doesn't help to expand the domain, then get certain amounts of expectations going, that it oughta be doing this and that and then be disappointed, when it fails. Read the sentence in context with the previous discusssion, it will make sense.

You obviously didn't understand what Marco was saying.


What I'd like to add is this. Sharing a return value between caller and method ideally should be fail safe.

There are several levels to this statement.

At the most general level you are asking for referential transparency, something you are never ever going to get in an imperative language, and Objective-C is always going to remain an imperative language.

However, I think it is very good that you did bring it up in this generality, because I think that this is the (somewhat hidden) motivation behind some of the things that are being said. I find this motivation understandable, because referential transparency is a nice thing to have. However, the only way to actually get referential transparency is to have a language designed from the ground up to support it. It is one of these things that you absolutely cannot bolt on to a language after the fact. How this usually works is that there are *no* references whatsoever, only values. So there can never be aliasing of pointers/references because there are no pointers/references. Conceptually, everything is copied all the time. So if you insert an element into a collection, a new collection is created that has the element inserted at the right spot. The old collection is not modified. Nothing is ever modified.

I ain't asking for nothing here :), I am pretty much completely happy with Objective-C as it is, because I can achieve what you call "referential transparency" _if_ _I_ _want_ _to_.

Once again, that is the big misconception. You cannot get referential transparency. Objective-C has side-effects. End of story,

But in many cases I am very happy that the language doesn't force me to do that. That's why you and me aren't coding Java (except for money) :)

Java doesn't have referential transparency either, not by a long shot.


For those int, double return values it's trivial, because the value is copied through registers or stack. If there is memory involved then there needs to be a clear protocol for the transfer or sharing of that memory.

It isn't really the memory that is a problem. It is references to values (vs. values) and the aliasing that can happen when you are allowed to have more than one reference to a value.

This is just the same. The references always point to memory.

That's an implementation detail. The problem is not the memory, it is the aliasing. If we have two references to things that are not memory (two file handles, for example) the same thing happens.

If the memory is owned by the caller, then there is no problem, the caller temporarily grants read/write access to its memory area, and can peruse it at will on return:

- (void) getName:(NSMutableString *) s
{
[s setString:name_];
}

Yes. However, I think you are forgetting that we are dealing with objects. Just as it is valid for one method to grant access to its 'memory area' to another method, it is just as valid for one *object* to grant access to its 'memory area' (instance variables) to another object.

I am afraid you don't follow my argument here.

Quite the opposite, it appears...

I was at that paragraph exactly just talking about the caller providing the memory,

Yes you were. I was saying that you are focusing on the call stack, which is not appropriate in an OO environment.

not the other way round (that's for later). Calling it object instead of memory doesn't make it any different, more complex maybe,

No, it focuses on the concept instead of implementation details that obscure what is actually happening.

this is ideal and fail safe. It is not done, because it is deemed to cumbersome. The caller must create temporary objects and release >> them.

It is not fail-safe, but it is cumbersome.

According to my discussion, it is fail safe.

Your discussion is wrong. There is nothing fail-safe about it.

The memory area was provided for the method as a return value. If the method chooses to retain it, it's breaking the contract. It's equally breaking the contract if it does memset( s, 0, 16) on it.

Precisely! Relying on others to not do something they *can* do is not fail-safe. It is fail-safe if it does not fail no matter what others do.


If the memory is solely owned by the method (instance), this means a weakening in the fail-safeness

I don't agree with this. It is sharing, just like your other case, except that it is the *object* doing the sharing, not the method.

Well looking from the caller (and that is the way I am looking at it in this discussion)

You are looking at it procedurally...

In the first case, you also transfered a pointer to memory, from the caller to the callee. This is just as unsafe because the callee could have released the memory. There is no difference!

Yeah could have, but that's "make pretend".

Not any more than your discussion. The release can happen just as easily indirectly, for example by the callee setting an instance variable of another object, where the argument it received was the previous value fo that instance variable. It is aliasing, you cannot get around that.

Marcel

--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
1d480c25f397c4786386135f8e8938e4
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Memory Management (Part I)
      • From: Marco Scheurer <email@hidden>
  • Prev by Date: Loading 2 classes with same name
  • Next by Date: Re: Learning Mac programming (Cocoa)
  • Previous by thread: Re: Loading 2 classes with same name
  • Next by thread: Re: Memory Management (Part I)
  • Index(es):
    • Date
    • Thread