I am working on an extension to Vienna to add the ability for third parties to write Objective-C plugins, but I am getting some runtime linker issues only when running in 64-bit mode (everything appears to work fine in 32-bit mode). My plugin, SynkPlugin, is loaded by the following code in Vienna.app:
NSArray * bundlePaths = [NSBundle pathsForResourcesOfType:#"bundle" inDirectory:[[Preferences standardPreferences] pluginsFolder]];
NSEnumerator * enumerator = [bundlePaths objectEnumerator];
NSString * bundlePath;
NSMutableArray * plugins = [NSMutableArray array];
while ( (bundlePath = [enumerator nextObject]) != nil )
{
NSBundle * pluginBundle = [NSBundle bundleWithPath:bundlePath];
Class principalClass = [pluginBundle principalClass];
id <ViennaPlugin, NSObject> plugin = [[principalClass alloc] init];
[plugins addObject:plugin];
[plugin release];
NSLog(#"Loaded plugin %# [main class: %#]", bundlePath, principalClass);
}
And in the console output, I get the following error message:
2010-07-09 08:55:40.128 Vienna[74065:a0f] Error loading /Users/dcrosta/Library/Application Support/Vienna/PlugIns/SynkPlugin.bundle/Contents/MacOS/SynkPlugin: dlopen(/Users/dcrosta/Library/Application Support/Vienna/PlugIns/SynkPlugin.bundle/Contents/MacOS/SynkPlugin, 265): Symbol not found: _OBJC_CLASS_$_Article
Referenced from: /Users/dcrosta/Library/Application Support/Vienna/PlugIns/SynkPlugin.bundle/Contents/MacOS/SynkPlugin
Expected in: flat namespace
in /Users/dcrosta/Library/Application Support/Vienna/PlugIns/SynkPlugin.bundle/Contents/MacOS/SynkPlugin
This error only happens when running in 64-bit mode, not 32-bit mode. Both Vienna and SynkPlugin are compiled with the "standard 32/64 universal" settings, and SynkPlugin has the additional linker flag "-undefined dynamic_lookup", which, it is my understanding, allows it to link to classes found in Vienna.app without having to compile code for those classes into its own binary.
The other references to this error on Stack Overflow have to do with UIKit differences between iPhone/iPod Touch and iPad -- in those cases, the frameworks are actually missing the classes on iPhone/iPod Touch. In my case, I'm certain that Vienna.app has the Article class within it somewhere, since it is built from identical code to the 32-bit version.
Has anyone seen an error like this before? Have any suggestions on where to look for more information? Thanks.
In the build settings of the Vienna target, under GCC 4.2 - Code Generation uncheck Symbols Hidden by Default (GCC_SYMBOLS_PRIVATE_EXTERN).
Related
I have an Objective-C iOS app that is composed of a workspace (AuthPort.xcworkspace) with a project (AuthPort.xcodeproj) and an Objective-C SDK project ("ANet SDK.xcodeproj") which builds a library called “libAnet SDK.a”).
“libAnet SDK.a” is linked in with the AuthPort target Build Phases.
In the SDK, there are some categories on NSString which get built into “libAnet SDK.a”. They are named:
NSString+stringWithXMLTag.h and NSString+stringWithXMLTag.m
.
.
.
etc.
Some code in the AuthPort.xcodeproj calls code in the library which in turn calls these category methods.
It all works smoothly in the above Objective-C environment!
Now…I am re-writing the AuthPort code in Swift in a new workspace with a new project and also using the Objective-C SDK which builds “libAnet SDK.a”
So, I created a new Swift Xcode project called AuthPort.xcodeproj and added it to a new workspace called AuthPort.xcworkspace. I also added the authorize.net SDK ("ANet SDK.xcodeproj", written in Objective-C) to the project.
So far, so good. I created an AuthPort-Bridging-Header.h file and imported all the headers from the SDK (without the category headers - more on this later). I also linked in “libAnet SDK.a” in the Build Phases of the AuthPort target.
AuthPort builds and runs with no errors or warnings.
Now, in viewDidLoad of the primary view controller I added code to access library methods to register a new device with the company, authorize.net:
var r: MobileDeviceRegistrationRequest = MobileDeviceRegistrationRequest()
an.mobileDeviceRegistrationRequest(r)
These two calls are the same library methods in both Objective-C and Swift versions. The last method calls other library methods which call methods from the NSString category above.
When I put breakpoints in a category method in the Objective-C environment (all Objective-C code in app and library), the program breaks as expected and I can step through the code.
When I put breakpoints in a category method in the Swift environment (Swift app project with Objective-C library), the debugger never enters a category method. At one point in the execution, the app SIGABRT’s with the following:
2015-04-21 12:25:47.388 AuthPort[4743:2404231] +[NSString stringWithXMLTag:andValue:]: unrecognized selector sent to class 0x38450e18
2015-04-21 12:25:47.390 AuthPort[4743:2404231] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSString stringWithXMLTag:andValue:]: unrecognized selector sent to class 0x38450e18'
* First throw call stack:
(0x29a0a5a7 0x3792fc77 0x29a0f989 0x29a0d949 0x2993eb68 0xd9e4b 0xc301f 0xdd507 0xc4725 0xc44b1 0xb8f28 0xb9e60 0x2cf0eb65 0x2cf0e8d5 0x2d031c43 0x2d031ac7 0x2d03196b 0x2cf1b5f1 0x2d0357b3 0x2d03491f 0x2cf2433b 0x2cf159e1 0x2cf15003 0x2cf14e6b 0x2cf148ef 0x2cf121ef 0x2cf7c031 0x2d16e34f 0x2d170781 0x2d17b1a9 0x2d16f063 0x302880a1 0x299d125d 0x299d0521 0x299cecbf 0x2991bb51 0x2991b963 0x2cf72b8f 0x2cf6dc91 0xb5290 0x37ed8aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
The [NSString stringWithXMLTag:andValue:] is from the category added by the library onto NSString.
The library method stringOfXMLRequest calls the category method stringWithXMLTag:andValue:
- (NSString *) stringOfXMLRequest {
NSString *s = [NSString stringWithFormat:#""
#"<merchantAuthentication>"
#"%#" //name (optional)
#"%#" //fingerPrint type Object #"%#" //transactionKey or
#"%#" //sessionToken or
#"%#" //password
#"%#" //mobileDeviceId (optional)
#"</merchantAuthentication>",
(self.name ? [NSString stringWithXMLTag:#"name" andValue:self.name] : #""),
(self.fingerPrint ? [self.fingerPrint stringOfXMLRequest] : #""),
(self.transactionKey ? [NSString stringWithXMLTag:#"transactionKey" andValue:self.transactionKey] : #""),
(self.sessionToken ? [NSString stringWithXMLTag:#"sessionToken" andValue:self.sessionToken] : #""),
(self.password ? [NSString stringWithXMLTag:#"password" andValue:self.password] : #""),
(self.mobileDeviceId ? [NSString stringWithXMLTag:#"mobileDeviceId" andValue:self.mobileDeviceId] : #"")];
return s;
}
The SIGABRT doesn't happen until the second call is made to stringWithXMLTag:andValue: for the transitionKey.
The exact same library code is run in both environments.
Here’s the code from stringWithXMLTag:andValue: which is never seen in the debugger:
+ (NSString *)stringWithXMLTag:(NSString *)t andValue:(NSString *)v {
if (v == nil) {
return #"";
}
else {
v = [NSString stringWithEscapedXMLValue:v];
return [NSString stringWithFormat:#"<%#>%#</%#>", t, v, t];
}
}
The problem is solely within the library code where a library method calls another library method which calls a library category method which is never entered in the debugger in the Swift version.
I also tried adding the category header files in the bridging header and adding -ObjC and -all_load to the Other Linker Flags for AuthPort. No luck.
I’ve searched the net for 2 days with no luck. Hopefully one of you will have seen this behavior.
I'm building using Xcode 6.3 and iOS 8.3 (deploying on iOS 8.2).
Thanks.
Check that:
you've added you category to the Umbrella Framework of your Framework project. Use #import "your.h"
I've marked the .h file from the category as public. Select the .h > Identity and Type > Target Membership > Public
In your project, set correct Framework Search Paths
Update: This was fixed in iOS 6.1 DP3 SDK.
I've tracked down a use-after-deallocated crash when building with ARC using the default release build configuration (debug seems to work fine). The problem occurs when creating an object inside an if-scope with a non-constant condition, assigning it to a variable from outside the scope and then only referencing the variable using Objective-C array or dictionary literals.
Here is the smallest reproducible case I've managed to find:
void test(BOOL arg)
{
id obj = nil;
if (arg) {
obj = [NSObject new];
}
// obj already deallocated here
#[obj];
// but using NSArray works
//[NSArray arrayWithObject:obj];
// #[obj] works if obj is referenced i.e. by NSLog print out
//NSLog(#"%#", obj);
}
int main(int argc, const char * argv[])
{
#autoreleasepool {
test(YES);
}
return 0;
}
When I build and run this with zombie objects enabled I get this error message:
-[NSObject retain]: message sent to deallocated instance 0x100109100
As I commented in the code it works fine if obj is referenced in some other way, like with NSLog or using NSArray instead. Have I misunderstood how objects are released with ARC and scopes or is this a optimization bug in LLVM or Clang?
I'm using Xcode 4.5.2 with clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn). I can reproduce it when building for x86 64 bit for iOS simulator and Mac OS X and I'm quite sure the same problem occurs when building for ARM as the issue was first found when running release build on an iPhone.
I have filed and bug report to Apple and created an open radar report.
What, if anything, am I missing?
Update, did some more experiments:
As Gabro pointed out the compiler translates #[] to a [NSArray arrayWithObjects:count:] statement so I did some tests:
// works
id a[] = {obj};
[NSArray arrayWithObjects:a count:1];
// does not work
const id *b = (id[]){obj};
[NSArray arrayWithObjects:b count:1];
// does not work
[NSArray arrayWithObjects:(id[]){obj} count:1];
So my guess is that this happens when combining ARC and anonymous C arrays.
You're not missing anything. It's a compiler bug.
I just tested the following code both building for OSX (x86 64) and iOS Simulator and I cannot reproduce the bug
void test(BOOL arg) {
id obj = nil;
if (arg) {
obj = [NSObject new];
}
#[obj];
NSLog(#"Hi there");
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
test(YES);
}
return 0;
}
The above code simply prints Hi there in the console and returns.
My configuration is the same of yours: XCode 4.5.2 and Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn) as a compiler.
EDIT
I also tried to compile from command line (after adding #include <Foundation/Foundation.h>
at the beginning of the above example) using
clang -fobjc-arc -framework Foundation main.m
And the result was again
2012-12-03 12:47:45.647 a.out[39421:707] Hi there
EDIT 2
As pointed out in the comments it is possible to reproduce the bug increasing the optimization level over -O0. Summarizing:
clang -O0 -fobjc-arc -framework Foundation main.m
the program works as expected
clang -O1 -fobjc-arc -framework Foundation main.m
the bug presented in the question shows up. This is true with any optimization level over -O0
It's definitely a bug in the compiler.
I'm trying to create an ObjC application that will control iTunes. I need a method that will return an array of all the playlists in iTunes.
I'm getting the most bizarre, unhelpful error message ever... First the code:
#import "MusicControl.h"
#import "iTunes.h"
#implementation MusicControl
- (SBElementArray *) playlists {
// Create iTunes Object
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:#"com.apple.iTunes"];
NSArray *sources = [iTunes sources];
iTunesSource *librarySource = nil;
for (iTunesSource *source in sources) {
if ([source kind] == iTunesESrcLibrary) {
librarySource = source;
break;
}
}
return [librarySource userPlaylists];
}
#end
I have no idea whether the array return is working or not because, after doing some debugging, I found that where this is bombing out is the very first line where I create the iTunes object, which was copied and pasted from Apple's website...
The error I'm getting is:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_SBApplication", referenced from:
objc-class-ref in MusicControl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any suggestion as the what the heck is going on?
This message (and similar ones) means that the linker is looking for some specific symbol, but can't find it. In your case it is SBApplication.
If you have not already done so, you should make sure that you have linked to the ScriptingBridge framework.
To add a framework, click on the project's icon at the top of the left hand bar in Xcode, then select Build Phases. If Link With Binary Libraries is not already expanded, do that and add the framework.
The same procedure can be used for plain libraries (a framework is really just a wrapper around a library, at least for the purpose of this discussion).
following code is copied from Stig Brautaset JSON library - I removed the comments to be more clear.
#interface NSString (NSString_SBJSON)
- (id)JSONFragmentValue;
- (id)JSONValue;
#end
I have a crash when I call:
NSString *jsonString = [request responseString];
...
NSDictionary *results = [jsonString JSONValue];//here is the crash with invalid selector.
I added this:
if (![jsonString respondsToSelector:#selector(JSONValue)])
{
NSLog(#"fix this!!!\n");
}
and the message is shown. Do you have any idea why this function in not called?
Thanks!
EDIT: I compile JSON framework as a static lib. The call is made in the same lib.
When I compile it as exe it runs without any problem.
You need to both link your static library and your final executable with the -ObjC linker option.
The -JSONValue method is implemented as a category on the NSString class in the file NSString+SBJSON.m file. Because of how the dynamic runtime works in Objective-C, categories defined in static libraries don't get loaded up unless you link with the -Objc flag. If the category doesn't get loaded, then dynamic binding fails, and you get the dreaded "doesn't respond to selector" exception.
I'm trying to compile a trivial command-line tool with XCode:
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//NSSpeechSynthesizer *speeker = [[NSSpeechSynthesizer alloc] initWithVoice: nil];
NSLog(#"%#", [NSSpeechSynthesizer availableVoices]);
NSLog(#"Hello, World!");
[pool drain];
return 0;
}
and even thought I'm importing Cocoa.h, I'm getting a link error:
Undefined symbols:
"_OBJC_CLASS_$_NSSpeechSynthesizer",
referenced from:
objc-class-ref-to-NSSpeechSynthesizer
in byc.o ld: symbol(s) not found
collect2: ld returned 1 exit status
Anybody knows what's going on???
You imported the header, so compilation worked, but linking failed because you didn't link against a framework that provides NSSpeechSynthesizer. You need to link against either the Application Kit framework (in addition to Foundation) or the Cocoa umbrella framework (instead of Foundation).
Whichever framework you choose, add it to your Linked Frameworks group in your project's group tree (by right-clicking on the group and choosing “Add Existing Framework”), and make sure you also add it to your target.