PackageDescription: StrongCompositePointers


Strong Composite Pointers

Last published: November 29, 2006 by 'michael'

Defines 1 Classes
Extends 2 Classes


This package provides a mechanism for holding on to strong references to parameters you place inside a C structure.

What does this mean? Say you have a CStruct that looks roughtly like this:

typedef struct mystruct {
char *name
} Mystruct;

And you call a method like this:

myMethod(*Mystruct);

You'll need to allocate an instance of the structure and free it once you're done. This can be done easily by calling:
mystruct := interface Mystruct gcMalloc.

But now you also need to give mystruct the name, eg:
mystruct := interface Mystruct gcMalloc.
mystruct memberAt: #name put: myObject name
interface myMethod: mystruct.

Unfortunately, Smalltalk does not guarantee that anObject name will be there by the time you do the myMethod call, so you need to move it to fixed space:
mystruct := interface Mystruct gcMalloc.
mystruct memberAt: #name put: myObject name asFixedArgument
interface myMethod: mystruct.

Unfortunately, Smalltalk may now garbage collect the FixedSpace copy of anObject name, so you need to hold that in a temporary variable too, eg:

| theNameCopy |
theName := anObject name asFixedArgument.
mystruct := interface Mystruct gcMalloc.
mystruct memberAt: #name put: theName.
interface myMethod: mystruct.

Or.. we can use this library, which simplifies it to the following:

mystruct := interface Mystruct gcMalloc.
mystruct strongMemberAt: #name put: anObject name.
interface myMethod: mystruct.

This library will hold on to a strong reference to the value in #name copied to fixed space for you until mystruct is garbage collected.