Objective-C

RSS for tag

Objective-C is a programming language for writing iOS, iPad OS, and macOS apps.

Posts under Objective-C tag

192 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

How to debug over-release in Objective-C with ARC?
I'm getting a runtime assertion failure like this: "<FFRender3DView 0x616000271580> has reached dealloc but still has a super view. Super views strongly reference their children, so this is being over-released, or has been over-released in the past." Looking at the code, I can't see any strong reference to the view except by its superview, so I can't see how it could be released other than by removal from its superview. My first instinct was to override release and set a breakpoint there, but that's not possible in ARC code.
2
0
567
Jul ’24
What is the reason for NSURLSession Trust evaluation fail
Hi All: Our use NSURLSession dataTaskWithRequest to connect our https server, However, in one macOS, sometimes, we encounter these error when TLS handshake. default 2024-06-24 17:52:03.054447 +0900 test-app boringssl_context_info_handler(2069) [C1209.1.1:2][0x7f9067117b10] Client handshake state: TLS client read_server_certificate info 2024-06-24 17:52:03.054462 +0900 test-app boringssl_session_handshake_incomplete(97) [C1209.1.1:2][0x7f9067117b10] Handshake incomplete: waiting for data to read [2] info 2024-06-24 17:52:03.054552 +0900 test-app boringssl_session_handshake_incomplete(97) [C1209.1.1:2][0x7f9067117b10] Handshake incomplete: waiting for data to read [2] info 2024-06-24 17:52:03.054557 +0900 test-app boringssl_session_handshake_incomplete(97) [C1209.1.1:2][0x7f9067117b10] Handshake incomplete: waiting for data to read [2] info 2024-06-24 17:52:03.054590 +0900 test-app boringssl_session_handshake_incomplete(97) [C1209.1.1:2][0x7f9067117b10] Handshake incomplete: waiting for data to read [2] default 2024-06-24 17:52:03.054769 +0900 test-app boringssl_context_info_handler(2069) [C1209.1.1:2][0x7f9067117b10] Client handshake state: TLS client read_certificate_status default 2024-06-24 17:52:03.054773 +0900 test-app boringssl_context_info_handler(2069) [C1209.1.1:2][0x7f9067117b10] Client handshake state: TLS client verify_server_certificate default 2024-06-24 17:52:03.055123 +0900 test-app boringssl_context_evaluate_trust_async(1635) [C1209.1.1:2][0x7f9067117b10] Performing external trust evaluation default 2024-06-24 17:52:03.055308 +0900 test-app boringssl_context_evaluate_trust_async_external(1620) [C1209.1.1:2][0x7f9067117b10] Asyncing for external verify block info 2024-06-24 17:52:03.055316 +0900 test-app boringssl_session_handshake_incomplete(97) [C1209.1.1:2][0x7f9067117b10] Handshake incomplete: certificate evaluation result pending [16] default 2024-06-24 17:52:03.055466 +0900 test-app Connection 1209: asked to evaluate TLS Trust default 2024-06-24 17:52:03.056082 +0900 test-app Task <407E11A6-12E8-4818-82B4-BC5B4909130F>.<1405> auth completion disp=1 cred=0x0 default 2024-06-24 17:52:03.064388 +0900 test-app Trust evaluate failure: [leaf SSLHostname TemporalValidity] default 2024-06-24 17:52:03.064390 +0900 test-app System Trust Evaluation yielded status(-9802) error 2024-06-24 17:52:03.064392 +0900 test-app ATS failed system trust error 2024-06-24 17:52:03.064393 +0900 test-app Connection 1209: system TLS Trust evaluation failed(-9802) default 2024-06-24 17:52:03.064393 +0900 test-app Connection 1209: TLS Trust result -9802 error 2024-06-24 17:52:03.064395 +0900 test-app Connection 1209: TLS Trust encountered error 3:-9802 error 2024-06-24 17:52:03.064397 +0900 test-app Connection 1209: encountered error(3:-9802) default 2024-06-24 17:52:03.064400 +0900 test-app Connection 1209: cleaning up default 2024-06-24 17:52:03.064404 +0900 test-app Connection 1209: summary for unused connection {protocol="(null)", domain_lookup_duration_ms=0, connect_duration_ms=0, secure_connection_duration_ms=0, private_relay=false, idle_duration_ms=0} default 2024-06-24 17:52:03.064438 +0900 test-app [C1209 63DEF1F8-AC5F-4285-B32B-D3AE707C513A Hostname#229f20b3:443 tcp, url hash: 693c58e9, tls, definite, attribution: developer] cancel I found TLS Trust evaluation failed(-9802) this error. I checked server's certificate, it is ok. On this macOS, this issue happens sometimes, not always. Thanks for your feedback.
1
0
465
Jul ’24
Instruments false positives
I have been able to get rid of reported memory leaks in Instruments not by fixing a leak but by adding some lines (like releasing a previous nil object) that convinced Instruments it is not a leak. But I am stuck are some I cannot stop Instruments from reporting. It reports a leak in this code if(expr.length&gt;0) { // add previous Atomic Atomic *atom = [[Atomic alloc] initWithString:[string substringWithRange:expr] isNumber:aNum]; [exprTokens addObject:atom]; [atom release]; } } and it underlines the substringWithRange method. I changed code to explicitly release atom and I assumed substringWithRange would return an autoreleased new string. I am aware Instruments tells you where the leaked object is created and not where it is leak, but only things created here are atom and a sub string and atom is released immediately. The initWithString method might do it, but I don't find anything there. Also, while running instruments, this code is 8542 times (I am pretty sure always in the same context) and Instruments says it only leaks 61 times. I don't now if there is misunderstanding are a code problem?
1
0
593
Jul ’24
Memory leak in ARC conversion, KVO involved
I'm trying to do a piecemeal conversion of a big macOS Objective-C++ code base to use Automatic Reference Counting (ARC), and started with a fairly complex modal dialog. I converted all the classes involved to use ARC. When the dialog closes, the window itself, and some of the controller objects, get deallocated as they should, but some do not. When I look at the memory debugging graph in Xcode, I see a bunch of things of the form NSKVONotifying_MyClassName. Here's an example: It does not look as though any of my objects have strong references to GRMorphController, so what am I to make of this?
2
0
556
Jun ’24
dateFromString results in nil
In a completely new project using Objective-C, when using "NSDateFormatter" under the conditions mentioned, setting initWithLocaleIdentifier to "NSCalendarIdentifierGregorian" results in "dateFromString" returning nil. This issue is occurring specifically in iOS 18 beta 1 and 2, and it's causing me significant trouble. This process works correctly on iOS 17 and earlier versions. NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeStyle:NSDateFormatterFullStyle]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSCalendarIdentifierGregorian]]; [formatter setDateFormat:formatStr]; NSDate *date = [formatter dateFromString:formatStr]; "date" is nil.
4
2
675
Jul ’24
About "SIMInserted" API
I am using the SIMInserted API on Xcode 16 beta. However, when I checked with a SIM card inserted, it returned "No". [Enviroment] Xcode:16beta iOS:18beta1,18beta2 [The modified implementation area is as follows] 1.Add "CarrierDescriptors" to the plist. <key>CarrierDescriptors</key> <array> <dict> <key>MCC</key> <string>440</string> <key>MNC</key> <string>10</string> </dict> </array> 2.Add "SIM Inserted for Wireless Carriers" to the capabilities. <key>com.apple.developer.coretelephony.sim-inserted</key> <true/> 3.In case of iOS 18 and above, perform SIM detection using "isSIMInserted" of CTSubscriber. - (BOOL)isSIMInseted { if(@available(iOS 18.0,*)){ CTSubscriber* ctSubscriber = [CTSubscriber new]; return = ctSubscriber.isSIMInserted; } return NO; } Is there any mistake in the implementation steps you provided? Why is it not possible to retrieve the desired information with this implementation? Please assistant me.
6
2
987
Jul ’24
Delete button in default NSSavePanel for new document
I just noticed that when closing a new document with edits in MacOS Sonoma that it skips the Save/Don't Save/Cancel panel and goes directly to default NSSavePanel with Delete/Cancel/Save buttons. The problem is that when I click "Delete" nothing happens. It should have simple solution, but I could not find anything. How does one respond to the "Delete" button? My undocumented (as far as I can tell) hack was to implement document:didSave:contextinfo selector for runModalSavePanelForSaveOperation. It appears that in this method for a new document: Delete button has didSave=YES (even though it did not save) and the document fileURL nil Cancel button has didSave=NO and document fileURL nil Save button has didSave=YES and document filieURL to saved file I can handle Delete button this way, but since it is not a documented method, it make me uncomfortable. For example what happens is user clicks "Save", but the save has an error? As an aside, since Apple is now working with ChatGPT, I thought it might provide some help. I asked it how I can respond to "Delete" button in MacOS Sonoma and it said to implement deleteDocument: in your NSDocument subclass. I pointed out to ChatGPT that deleteDocument: does not exist. It said "you are correct" and you should instead check the returned result from runModalSavePanelForSaveOperation and look for "stop" action. I pointed out to ChatGPT that runModalSavePanelForSaveOperation is void and does not return a result, it said again, "you are correct." It gave a third option which basically said to override runModalSavePanelForSaveOperation and build your own save panel from scratch. I didn't know if I should trust this answer. I reverted to my hack and wrote this post. Also ChatGPT never apologized for wasting my time with the wrong answers.
3
0
679
Jun ’24
Xcode16 Beta Compilation Error: Reserved Keyword 'module' Issue
While compiling with the Xcode Beta1 version, the 'module' keyword used in the previous method as a parameter is now recognized as a reserved keyword, causing compilation errors and preventing our project from continuing to run. Please fix this issue as soon as possible. Additionally, I would like to vent: It's quite uncomfortable that the MacOS Beta version cannot be downgraded directly. I am now facing the dilemma of being unable to open Xcode 15 and having Xcode 16 fail to compile.
4
0
1k
Jun ’24
macOS app crashes on first launch from TestFlight
2024_06_09_testflight_launch_crash_log.txt Running my macOS app the first time after installing via TestFlight crashes on launch. Every subsequent run works fine (including completely quitting the app and re-running it). Also, building and running directly in XCode both in Debug and Release mode works fine. I'm on a Mac M2 and the app excludes x86_64 arch. Here is the trimmed crash log (sanitised full log is attached) Crashed Thread: 0 Dispatch queue: com.apple.main-thread Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000, 0x0000000000000000 Termination Reason: Namespace SIGNAL, Code 6 Abort trap: 6 Terminating Process: CamHero-macOS-v2-Release [12876] Application Specific Information: abort() called Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libsystem_kernel.dylib 0x18c81d11c __pthread_kill + 8 1 libsystem_pthread.dylib 0x18c854cc0 pthread_kill + 288 2 libsystem_c.dylib 0x18c764a40 abort + 180 3 libglib-2.0.0.dylib 0x1061e5bd0 g_assertion_message + 464 4 libglib-2.0.0.dylib 0x1061e5c44 g_assertion_message_expr + 116 5 libgstlibav.dylib 0x10549ede0 gst_ffmpeg_cfg_init + 776 6 libgstlibav.dylib 0x10548a4f4 plugin_init + 140 7 libgstreamer-1.0.0.dylib 0x10646a458 gst_plugin_register_func + 636 8 libgstreamer-1.0.0.dylib 0x106469fe4 gst_plugin_register_static + 212 9 libgstlibav.dylib 0x10548a45c gst_plugin_libav_register + 92 10 CamHero-macOS-v2-Release 0x104a01624 gst_ios_init + 900 (gst_ios_init.m:1072) 11 CamHero-macOS-v2-Release 0x104a80590 specialized CamHeroMacOSApp.init() + 68 (CamHeroMacOSApp.swift:19) 12 CamHero-macOS-v2-Release 0x104a804a4 CamHeroMacOSApp.init() + 4 [inlined] 13 CamHero-macOS-v2-Release 0x104a804a4 protocol witness for App.init() in conformance CamHeroMacOSApp + 20 (<compiler-generated>:16) 14 SwiftUI 0x1b7f134e0 0x1b7163000 + 14353632 15 CamHero-macOS-v2-Release 0x104a804d8 static CamHeroMacOSApp.$main() + 24 [inlined] 16 CamHero-macOS-v2-Release 0x104a804d8 main + 36 17 dyld 0x18c4d90e0 start + 2360 Thread 1: 0 libsystem_pthread.dylib 0x18c84fe28 start_wqthread + 0 Thread 2: 0 libsystem_pthread.dylib 0x18c84fe28 start_wqthread + 0 Thread 0 crashed with ARM Thread State (64-bit): x0: 0x0000000000000000 x1: 0x0000000000000000 x2: 0x0000000000000000 x3: 0x0000000000000000 x4: 0x0000600000755680 x5: 0x0000000000000020 x6: 0x0000600000755680 x7: 0xffffffff00008600 x8: 0x8ddd1ea266cd729b x9: 0x8ddd1ea3846d2c5b x10: 0x0000000000001620 x11: 0x00000000dbe367fb x12: 0x00000000000007fb x13: 0x00000000000007fd x14: 0x00000000dc03683c x15: 0x000000000000003c x16: 0x0000000000000148 x17: 0x00000001eb9f3da0 x18: 0x0000000000000000 x19: 0x0000000000000006 x20: 0x00000001e2a05ec0 x21: 0x0000000000000103 x22: 0x00000001e2a05fa0 x23: 0x000000016b407508 x24: 0x0000000000000000 x25: 0x00000001064f8450 x26: 0x000060000080d511 x27: 0x0000000000000000 x28: 0x0000000000000000 fp: 0x000000016b407460 lr: 0x000000018c854cc0 sp: 0x000000016b407440 pc: 0x000000018c81d11c cpsr: 0x40001000 far: 0x0000000000000000 esr: 0x56000080 Address size fault It seems that the initialisation of one of the gstreamer plug-ins doesn't go well. But, I wonder what might lead to that happening only the first time after launch? Will appreciate any clues! EDIT / UPDATE: If i remove the app completely and install the same version again from TestFlight, app doesn't crash. So it seems the crashing is limited to the very first launch of a new /updated version.
3
0
660
Jun ’24
How do you allow an XPC service to create a new file based on an NSURL that the user selected from an NSSavePanel?
How do you send an NSURL representing a new file, as returned from an NSSavePanel, to an XPC service such that the service is granted permission to create the file? I can successfully pass an NSURL to the XPC process if the NSURL represents an existing file. This is documented in Apple's Documentation: Share file access between processes with URL bookmarks This involves creating bookmark date while passing 0 in as the options. However, if you try to create bookmark data for an NSURL that represents a file that is not yet created, you do not get any bookmark data back and an error is returned instead: Error Domain=NSCocoaErrorDomain Code=260 "The file couldn’t be opened because it doesn’t exist." Simply passing the file path to the XPC process, by way of: xpc_dictionary_set_string(message, "file_path", url.fileSystemRepresentation); Does not grant the XPC create/write permissions. Is there an API or trick I'm missing? Note that the user should be allowed to save and create new files anywhere of their choosing, thus restricting URLs to only those within a group or container shared between the app and service isn't really viable. Using the latest of everything on macOS with the xpc_session API...
1
0
584
Jun ’24
NSString.getBytes does not crash even when an invalid range is passed.
When we pass some special words, NSString.getBytes does not crash even when we pass an invalid range. It seems a bug. The below code is an example. func testNSStringGetBytes() { let originalString: String = "􁜁あ" let bufferSize = 256 var buffer = [UInt8](repeating: 0, count: bufferSize) var usedLength = 0 // An invalid range is passed let range = NSRange(location: 0, length: originalString.count + 1) var remainingRange = NSRange() (originalString as NSString) .getBytes( &buffer, maxLength: bufferSize, usedLength: &usedLength, encoding: String.Encoding.utf8.rawValue, options: [], range: range, remaining: &remainingRange ) print("Used Length: \(usedLength)") print("Buffer: \(buffer[0..<usedLength])") if remainingRange.length > 0 { print("Did not convert the whole string. Remaining range: \(remainingRange)") } else { print("Entire string was converted successfully.") } }
1
0
421
Jun ’24
Archiving my macOS target app fails with BOOL error
I'm building a macOS target for my App (which also has some Obj-C code). Building and running the app is fine but when I archive the app in XCode, the process / build fails with the following error Type 'BOOL' (aka ;Int32') cannot be used as a boolean;test for '!=0' instead It happens in a couple of places, one of the places being private func getRootDirectory(createIfNotExists: Bool = true) throws -> URL { return try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) } where it complains that create: true is not acceptable and throws the above error. If I comment out this line, the archive works successfully. When i Cmd + click the definition of Filemanager.default.url , i get this @available(macOS 10.6, *) open func url(for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: BOOL) throws -> URL This looks fishy since it it says create shouldCreate: BOOL whereas the documentation says it should be just Bool func url( for directory: FileManager.SearchPathDirectory, in domain: FileManager.SearchPathDomainMask, appropriateFor url: URL?, create shouldCreate: Bool ) throws -> URL My minimum deployment target is macOS 13.0 I'm quite stumped at this error - which happens only while archiving. Does anybody know why?
4
0
616
Jun ’24
How activate window correctly using activationPolicy
I have the following code: + (BOOL)activateWindow:(NSWindow*)window { if (NSApp.activationPolicy != NSApplicationActivationPolicyRegular) [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; if (window) { [NSApp activate]; //if (window.isMiniaturized) [window deminiaturize:nil]; [window makeKeyAndOrderFront:nil]; } return YES; } + (BOOL)hideWindowFromDock:(NSWindow*)window { if (NSApp.activationPolicy != NSApplicationActivationPolicyProhibited) [NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited]; window.isVisible = NO; return YES; } I hide app main window by setting NSApplicationActivationPolicyProhibited if it is minimized or being closed. The code worked most of the time. But since upgrading to Sonoma, sometimes it won't correctly activate main window. The window icon reappears in the dock, but the window won't show up. I have to click on the window icon again to let it 'order front'. Sometimes, I observe a very weird behavior of the window being activated. It 'order front' and then disappears and re-appears.
1
0
594
Jun ’24
Problem with setNeedsDisplay:
Hi, with the default values the rotation take place but changing the value not. I bind a button to a slider action: -(IBAction)rotateXAction:(id)sender { NSLog(@"%@ \n",sender); BOOL yn = YES; _rotationX = [_sliderX intValue]; if(yn) printf("rotationX %d \n",_rotationX); // value o.k [self setNeedsDisplay:YES]; } The drawRect: will not be called. What is wrong with my code, please tell me. Uwe
1
0
635
Jun ’24
Cannot get NSTimer working in another thread
I need to create timers in another thread (not on main): @implementation AppDelegate { NSThread* _worker; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { _worker = [[NSThread alloc] initWithTarget:self selector:@selector(main) object:nil]; [_worker start]; } - (void)main { [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer_tick:) userInfo:nil repeats:YES]; [NSThread sleepForTimeInterval:INFINITY]; NSLog(@"**********************"); } - (void)timer_tick:(NSTimer*)timer { NSLog(@"%@", NSThread.currentThread); } The timer never gets fired. What's wrong with my code?
2
0
563
May ’24