Closed novixon closed 8 years ago
Hello @novixon! Can you give me some more details about your implementation a factory method with block?
Hi @spbvasilenko! Here it is. It not the exact production code, but it is pretty identical. As I said before there is no crash and the code works fine If I don't use localVariable inside a runtime-injected block.
typedef void(^ResultBlock)(id data, NSError *error);
- (void)configureWithResultBlock:(ResultBlock)resultBlock;
- (id)factoryMethodWithBlockRuntimeArgument:(ResultBlock)resultBlock {
return [TyphoonDefinition withClass:[SomeClass class]
configuration:^(TyphoonDefinition *definition) {
[definition useInitializer:@selector(defaultInitializer)];
SEL configSel = @selector(configureWithResultBlock:);
[definition injectMethod:configSel
parameters:^(TyphoonMethod *method) {
[method injectParameterWith:resultBlock];
}];
}];
}
@novixon hmm..it is strange behaviour. I'm written test code like your example and make some few tests, this work fine with local variables. Local variable is retained into block scope.
@spbvasilenko Check it with iPhone 4/4s/5 simulator/device (32-bit architecture). We've found that problem is hidden there.
@novixon Yep, Im reproduce this issue on 32-bit architecture. I have noticed interested behaviour...if result block is NSMallocBlock, code work fine without crashes. If block is NSStackBlock, we have a crash.
NSObject *object = [NSObject new];
__weak typeof(object) weakObject = object;
void (^block)(id data, NSError *error) = ^(id data, NSError *error) {
__weak typeof(object) strongObject = weakObject;
if (strongObject) {
NSLog(@"");
}
};
[_assembly factoryMethodWithBlockRuntimeArgument:[block copy]];
NSLog(@"%@", object);
This slice example code work without crash.
@spbvasilenko Yep, I got the same result - no crash when defining the result block before passing it as a parameter.
@novixon, I'm want to fix this issue and make the MR
@spbvasilenko It's great, thanks! I will research this a little bit on weekends if needed.
Reviewed. Looks cool! Thanks, guys!
Hello, guys! I found a problem.
I want to make a factory method with Typhoon. My TyphoonAssembly is OK - I checked it. The problem is though that I get "exc_bad_access" crash when using the client's code below.
I found that this crash happens when localVariable is released (e.g. in the end of the method). I thought that it has to be retained by the block. The crash will not appear if I make the localVariable static, for example.
If I don't use some kind of local variables inside block - my code works fine. Do you have any ideas on this problem?
Thanks!