Skip to content

SWIG: disown Python/Octave objects assigned to viewed C arrays *only* for pointer arrays

Description

This is a fixup to !1299 (merged), which was related to semantics around what happens to the memory of SWIG-wrapped objects when they are assigned to an element of a SWIG-wrapped array.

The SWIG wrappings are meant to follow the C semantics illustrated below:

typedef struct { ... } Foo;

typedef struct {
   int length;
   Foo* data;   // pointer to an array of Foo structs, laid out end-to-end in memory
} ArrayOfFoo;

typedef struct {
   int length;
   Foo** data;   // pointer of an array of pointers to Foo structs
} ArrayOfFooPtr;

Foo* x = malloc(sizeof(Foo));

ArrayOfFoo* y = CreateArrayOfFoo(...);
y->data[0] = *x;   // y->data[0] has struct-copied the value of x; x still "owns" the memory

ArrayOfFooPtr* z = CreateArrayOfFooPtr(...);
z->data[0] = x;   // z->data[0] has claimed the pointer to x; memory is now "owned" by z

The fix in !1299 (merged) fixed the semantics for arrays of type ArrayOfFooPtr, but broke the semantics for arrays of type ArrayOfFoo. This merge request should now fix both.

Tests for Octave and Python for the semantics for both array types are in LALPulsar, since it's convenient to use LALPulsar types to test the semantics.

Fixes #467 (closed)

API Changes and Justification

Backwards Compatible Changes

  • This change introduces no API changes
  • This change adds new API calls

Backwards Incompatible Changes

  • This change modifies an existing API
  • This change removes an existing API

Review Status

N/A

Edited by Karl Wette

Merge request reports