Getting a corrupt value reading an NSNumber unsignedLongLongValue
I was trying to get the PersistentID from iTunes tracks from both ScriptingBridge and the notifications iTunes posts to NSDistributedNotificationCenter. I noticed I was getting different results from each. The first byte from the number read from the notification is nearly always garbled (I suspect, only the first two bits). Here's a script that'll show it: #!/usr/bin/env macruby framework "Cocoa" framework 'ScriptingBridge' $itunes = SBApplication.applicationWithBundleIdentifier('com.apple.iTunes') def trackChange(notification) n = notification.userInfo["PersistentID"] # NSNumber puts "PersistentID from ScriptingBridge: %s" % $itunes.currentTrack.persistentID puts "PersistentID from notification...: %016X" % n.unsignedLongLongValue puts "===" end NSDistributedNotificationCenter.defaultCenter.addObserver(self, selector:"trackChange:", name:"com.apple.iTunes.playerInfo", object:nil) NSRunLoop.currentRunLoop.run Once running, it'll print the PersistentID from each source every time you skip a track in iTunes. Notice how the first digit tends to differ. Sample output: PersistentID from ScriptingBridge: C06AE8B41249F001 PersistentID from notification...: 006AE8B41249F001 === PersistentID from ScriptingBridge: E99C2A8ABA6C7C8F PersistentID from notification...: E99C2A8ABA6C7C8F === PersistentID from ScriptingBridge: 9993017C397E8601 PersistentID from notification...: 1993017C397E8601 === PersistentID from ScriptingBridge: 8C2CACEFCD8BBABA PersistentID from notification...: 0C2CACEFCD8BBABA Here's a similar Nu program that proves the data iTunes posts via the notification is originally correct: #!/usr/bin/env nush (load "Cocoa") (load "ScriptingBridge") (set $itunes (SBApplication applicationWithBundleIdentifier: "com.apple.iTunes")) (class MyObserver is NSObject (imethod (void) trackChange: (id) notification is (set v ((notification userInfo) "PersistentID")) (system "ruby -e 'puts %q[%016X] % #{(v unsignedLongLongValue)}'") (puts (($itunes currentTrack) persistentID)) (puts ""))) (set $observer (MyObserver new)) ((NSDistributedNotificationCenter defaultCenter) addObserver:$observer selector:"trackChange:" name:"com.apple.iTunes.playerInfo" object:nil) ((NSRunLoop currentRunLoop) run) And some output from it: 9566705CB7907985 9566705CB7907985 AC4BAAE6BE61E8B1 AC4BAAE6BE61E8B1 E4DBB66CFAE503E7 E4DBB66CFAE503E7 So can we say there's a bug here or am I missing something terribly obvious? Could someone try to reduce this and take the notification center and iTunes out of the picture and see if it's just a problem with NSNumber?
On 2010-12-20, at 16:37 , Caio Chassot wrote:
Could someone try to reduce this and take the notification center and iTunes out of the picture and see if it's just a problem with NSNumber?
Oh, that was easier than I thought. #!/usr/bin/env macruby n = 0x8C2CACEFCD8BBABA m = NSNumber.numberWithUnsignedLongLong n puts "%016X" % n puts "%016X" % m puts "%016X" % m.unsignedLongLongValue Gives: 8C2CACEFCD8BBABA 0C2CACEFCD8BBABA 0C2CACEFCD8BBABA
Hi Caio, Excellent! In this snippet, n is a 64-bit bignum, so I suspect something is going wrong in the converter. Could you file a ticket? We will get that fixed for 1.0. Laurent On Dec 20, 2010, at 12:49 PM, Caio Chassot wrote:
On 2010-12-20, at 16:37 , Caio Chassot wrote:
Could someone try to reduce this and take the notification center and iTunes out of the picture and see if it's just a problem with NSNumber?
Oh, that was easier than I thought.
#!/usr/bin/env macruby n = 0x8C2CACEFCD8BBABA m = NSNumber.numberWithUnsignedLongLong n puts "%016X" % n puts "%016X" % m puts "%016X" % m.unsignedLongLongValue
Gives:
8C2CACEFCD8BBABA 0C2CACEFCD8BBABA 0C2CACEFCD8BBABA
On 2010-12-20, at 19:22 , Laurent Sansonetti wrote:
Excellent! In this snippet, n is a 64-bit bignum, so I suspect something is going wrong in the converter. Could you file a ticket? We will get that fixed for 1.0.
https://www.macruby.org/trac/ticket/1068 I wonder if MRI ruby 1.9 is not also affected by this. Any pointers on how to test it?
On 2010-12-20, at 19:22 , Laurent Sansonetti wrote:
Excellent! In this snippet, n is a 64-bit bignum, so I suspect something is going wrong in the converter
I couldn't help but prod around in bignum.c but I'm in way over my head. How do you go about debugging this? I suppose I'd try to run that snippet under MacRuby attached to a debugger… but I don't even know how to get there. I know I may be asking you to describe over ten years of acquired programming skills, but I'll take anything, even a non-narrated screen capture of you non-didactically going about your own debugging of this. (Then again maybe you can just spot the bug by skimming the C source, so…)
participants (2)
-
Caio Chassot
-
Laurent Sansonetti