我正在为 iOS 编写一个简单的 Adobe 原生扩展。
它所做的一切都显示了一个 UISwitch。
但是,当我尝试在该 UISwitch(或这段代码中的任何 UIView)上设置框架时,甚至访问 UISwitch 的框架时,应用程序会因内存访问崩溃而出现段错误。UISwitch 本身不是 nil,因为在它上面调用setOn是有效的,如果我不尝试在它上面调用 setFrame,或者设置它的中心,或者以任何方式定位它, UISwitch 确实会显示在屏幕上。
这是代码。有问题的函数是第一个:showSwitch()
#import "FlashRuntimeExtensions.h"
#import <UIKit/UIKit.h>
FREObject showSwitch(FREContext context,
void* functionData,
uint32_t argc,
FREObject argv[])
{
int32_t isOn;
FREGetObjectAsInt32(argv[0], &isOn);
id delegate = [[UIApplication sharedApplication] delegate];
UIWindow* window = [delegate window];
// The following will segfault
// UISwitch* switchControl = [[[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
// This works fine.
UISwitch* switchControl = [[[UISwitch alloc] init] retain];
// This will segfault as well
// [switchControl setFrame:CGRectMake(100, 100, switchControl.frame.size.width, switchControl.frame.size.height)];
[switchControl setOn:(isOn == 1)]; // switchControl is not nil, because this works.
[window addSubview:switchControl];
return NULL;
}
void ContextFinalizer(FREContext context)
{
return;
}
void ContextInitializer(void* extensionData,
const uint8_t* contextType,
FREContext context,
uint32_t* numFunctionsToTest,
const FRENamedFunction** functionsToSet)
{
*numFunctionsToTest = 1;
FRENamedFunction* function = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);
function[0].name = (const uint8_t*)"showSwitch";
function[0].functionData = NULL;
function[0].function = &showSwitch;
*functionsToSet = function;
}
void ExtensionInitializer(void** extensionDataToSet,
FREContextInitializer* contextInitializerToSet,
FREContextFinalizer* contextFinalizerToSet)
{
*extensionDataToSet = NULL;
*contextInitializerToSet = &ContextInitializer;
*contextFinalizerToSet = &ContextFinalizer;
}
void ExtensionFinalizer(void* extensionData)
{
return;
}