On Mar 8, 2015, at 20:00 , Patrick J. Collins <[email protected]>
wrote:
>
> Why in the world am I losing the ks property?
We’ve been here before. :)
— ‘k[11]’ is a block of 11*sizeof(double) bytes, on the stack.
— ‘k’ is a pointer to this block
— “ks” is a Reflector property whose value is a pointer.
So, you set the property to a pointer, but the actual memory pointed to is
invalidated as soon as ‘translateCoefficients’ returns. Your solution of
malloc’ing the block is one way of solving this. Here’s another:
> @interface Reflector ()
> @property (nonatomic) double *ks;
> @end
>
> @implementation Reflector
> {
> double _ks[11];
> }
>
> -(instancetype)initWithKs:(double *)ks rms:(NSUInteger)rms {
> …
> for (NSUInteger i = 0; i < 11; i++)
> _ks [i] = ks [i];
> …
That is, put the memory for the known-sized array in the object itself, thereby
avoiding allocating a separate block. It’s still in the heap (because all
object memory is in the heap), but there’s no additional memory management
needed.
P.S. It’s generally bad form to use getters in an ‘init…’ method. It’s safer to
refer to the instance variable (_ks, _rms) directly instead.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com
This email sent to [email protected]