Go to the first, previous, next, last section, table of contents.


Memory

Guile uses a garbage collection system for memory management, while the Foundation library (gstep-base, libFoundation, or Foundation) uses a reference counting mechanism for objc objects.

Actually, libFoundation has a garbage collection mechanism too - I don't think you can use it with gstep-guile though - it might conflict with the Guile garbage collection system.

Most data items other than objc objects are of no concern - they are passed by copy rather than by reference, so the standard memory management systems within each world apply to the copies of each data item.

How to resolve the difference between the two worlds for objc objects?

The basic rules for OpenStep reference counting are - If an object is returned to you by an 'alloc', 'new', 'copy' or 'mutableCopy' method then you 'own' that object and do not need to retain it. Otherwise, you have to retain any object until you have finished with it.

So - when an objc object is returned to the world of guile by gstep-guile, a guile data item is created which is subject to the guile garbage collection mechanism. This item contains a pointer to the objc object which is 'retained' (it's reference count is increased by one) iff the object was not returned by a 'new', 'alloc', 'copy' or 'mutableCopy' method.

When the garbage collection mechanism decides that the data item is no longer in use, it is freed and a 'release' message is sent to the corresponding objc object (decreasing it's reference count). If the objc object is not in use by any other object in the objc world, it's reference count will drop to zero and it will be released.

Objc Class and Protocol objects either do not respond to [-retain] and [-release] methods, or implement them as no-ops. This means that these objects are neither retained nor released - so guile garbage collection has no effect on them. This is 'correct' behavior, since Class and protocol objects should normally never be deallocated.

This is all that needs to be done for clean inter-operation between the two worlds - guile data items are garbage collected as normal in their world, while objc objects are reference counted as normal in their world.

The result of this is that you normally do not need to bother about the OpenStep retain/release reference counting system - gstep-guile will do it all for you.

That's not all that gstep-guile does though ...

Gstep-guile keeps track of all objc objects for which a guile data item exists, and ensures that when an objc object is returned into the guile world, a new guile data item is not created if one already exists.

This means that there is guaranteed to be at most one guile data item for each objc object and therefore each objc object is 'retained' only once by the guile world. This guarantee makes it easy to use the 'retainCount' method on an object with predictable and comprehensible results. This is important for checking your code for errors in retaining and releasing objects.

Tricky stuff

Actually the OpenStep reference counting system is not quite as simple as I made out above ... It is legitimate for an objects `init' methods to return a new object to replace it. In this case the objects init method will have released the object itself! Gstep-guile manages this by checking to see if the object returned from an init method is the same as the object to which the message was sent. If not, the Guile wrapper for the original object is fixed-up to contain a `nil' pointer and a new wrapper item is created for the newly returned object.

The `voidp' type is the other tricky aspect of Guile-Objective-C interaction. This type is used to pass pointers to arbitrary data and, as such, can be used to trash your memory! It must be used with care.

An Objective-C method whose return type is `void*' will return a `voidp' item to the Guile world. This item will contain a pointer to an address in memory, but that is all the information Guile will have about it.

As the programmer - you are responsible for making sure that this item is used correctly -


Go to the first, previous, next, last section, table of contents.