블록의 C 배열을 동적으로 인스턴스화하고로드 한 다음 실행하여 도움이 될 수 있습니다.C 스타일 배열에 블록 저장
// Definitions ===========================================
typedef void (^MorphC)(ScratchC* scratch);
@property (nonatomic) MorphC __strong * morphCs;
// Building up the Morph Registry ========================
static NSMutableDictionary* morphs_;
+ (void) initialize {
morphs_ = [[NSMutableDictionary alloc] init];
[MathC hydrate];
}
+ (void) hydrate {
[MathC registerMorph:@"sin" execute:^(ScratchC* scratch) {
AEScratchPush(scratch, sin(AEScratchPop(scratch)));
}];
}
+ (void) registerMorph:(NSString*)name execute:(MorphC)execute {
[morphs_ setObject:execute forKey:name];
}
+ (MorphC) morphFromKey:(NSString*)key {
return [morphs_ objectForKey:key];
}
// Loading up a temporary NSMutableArray* _compiling =====
- (void) applyTag:(NSString*)tag stack:(Stack*)stack {
[_compiling addObject:[MathC morphFromKey:tag]];
}
// Initializing C Array and loading from NSMutableArray ==
- (void) build {
_morphCs = (MorphC __strong *)malloc(_compiling.count*sizeof(MorphC));
i = 0;
for (MorphC morph in _compiling)
_morphCs[i++] = morph; // Currently, getting a bad ACCESS here
}
// Executing the Morphs ==================================
- (CGFloat) evaluateFloat:(VarsC*)vars {
if (![_morphs count]) return NAN;
AEScratchLoadVariables(_scratch, vars);
for (int i=0;i<[_morphs count];i++)
_morphCs[i](_scratch);
return AEScratchPop(_scratch);
}
현재 C 배열을 구축하는 동안 EXC_BAD_ACCESS가 표시되지만 많은 문제가 있다고 생각됩니다. morphCs 정의에서 __strong의 필요성을 완전히 이해하지는 못했지만 컴파일러는 불만을 토로합니다. 속성에도 강한 지표가 있어야합니까?
하나 이상의 장소에서 [모프 사본]을 수행해야합니까?
내가 망쳐 놓은 것이 있습니까?
그래서 해결책은'calloc'입니까? –
@BryanChen :'calloc'이 작동해야합니다. (플러스는'free'ing 전에'nil'에 대한 설정 포인터를 추가하고, 코드에서 자유로운 곳을 표시하지 않습니다.) – newacct
실제로 __weak에 대한 포인터를 설정하여이 작업을 수행 할 수있었습니다. 단순히 __weak으로 설정하고 메모리 관리를 직접 처리 할 수 있습니까? 또한 registerMorph 메서드에 'copy'를 추가했지만 그 밖에는 없습니다. 이 조합은 이제 제대로 작동하는 것 같습니다. 그러나, 나는 실제로 실제로 일어나고있는 것에 대해 꽤 불안하다. – aepryus