From dfe50b2e72ddbd0148870748975f00e7fc662314 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Tue, 12 Aug 2008 14:28:22 +0200 Subject: MLKCompiledClosure: Always indirect through a function pointer before doing a call. --- MLKCompiledClosure.h | 3 ++- MLKCompiledClosure.m | 14 +++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/MLKCompiledClosure.h b/MLKCompiledClosure.h index a4498d2..53267b2 100644 --- a/MLKCompiledClosure.h +++ b/MLKCompiledClosure.h @@ -27,8 +27,9 @@ @interface MLKCompiledClosure : NSObject { int _dataLength; - id (*_code)(); + id (**_code)(); id *_data; + BOOL _ownPointer; // do we own the _code pointer cell? } -(id) initWithCode:(void *)code diff --git a/MLKCompiledClosure.m b/MLKCompiledClosure.m index 35c8aca..b70fa03 100644 --- a/MLKCompiledClosure.m +++ b/MLKCompiledClosure.m @@ -38,7 +38,10 @@ _data = data; _dataLength = dataLength; - _code = code; + _ownPointer = YES; + + _code = malloc (sizeof (id (*)())); + *_code = code; for (i = 0; i < _dataLength; i++) { @@ -86,7 +89,7 @@ format:@"FFI type is invalid (this is probably a bug)."]; } - ffi_call (&cif, FFI_FN (_code), &return_value, (void**)argv); + ffi_call (&cif, FFI_FN (*_code), &return_value, (void**)argv); // FIXME return [NSArray arrayWithObject:nullify(return_value)]; @@ -108,11 +111,16 @@ [super dealloc]; - // FIXME: Decrease refcount of _code. + // FIXME: Decrease refcount of *_code. Note: When releasing *_code, + // also release _code regardless of whether we own it. + for (i = 0; i < _dataLength; i++) { LRELEASE (_data[i]); } free (_data); + + if (_ownPointer) + free (_code); } @end -- cgit v1.2.3