Memory allocation: Difference between revisions

Line 1,450:
 
{{works with|Smalltalk/X}}
To allocate a non-movable, non garbage collected block of memory;, (eg. to hand out a block of memory on which an external C-function keeps a reference). The memory must be eventually explicitly freed by the programmer:
<lang smalltalk>handle := ExternalBytes new:size
...
handle free</lang>
To allocate a non-movable block of memory, which is garbage collected as soon as the reference is no longer reachable by Smalltalk (useful to hand out a block of memory to an external function which does NOT keep a reference on it):
<lang smalltalk>handle := ExternalBytes unprotectedNew:size
...
handle := nil "or no longer reachable"
...
blockmemory will be freed by the garbage collector eventually</lang>
 
Of course, both are to be used with great care, as memory leaks are possible. Thus, it is only used by core parts of the system, eg. for async I/O buffers, shared memory, mapped I/O devices etc. Normal programs would not use them.
Anonymous user