How to disable the auto-reopening of the last document ?
When I execute my application, it calls the method readFromData.
But, the problem is that between my version 1 and my version 2, I changed the structure of the data that is saved by the application. In v1, the root object was an array. In v2, the root object is a dictionnary with two keys, one for a string, and one for an array.
When the application loads, it seems that it load from the data an array and then tries to get from this array an object for the keys of the dictionnary.
What shall I do !!??
PS : I tried to create a NSApplicationDelegate with
-(BOOL)applicationShouldOpenUntitledFile:(NSApplication*)app
{
return YES;
}
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender
{
return YES ;
}
but these methods are never called by my application.
EDIT :
Here is the call stack :
0 CoreFoundation 0x00007fff8bc06f56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff81f37d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8bc931be -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff8bbf3e23 ___forwarding___ + 371
4 CoreFoundation 0x00007fff8bbf3c38 _CF_forwarding_prep_0 + 232
5 dictionnaireDouchoutique 0x0000000100007f22 -[Document readFromData:ofType:error:] + 498
6 AppKit 0x00007fff89af3558 -[NSDocument readFromURL:ofType:error:] + 665
7 AppKit 0x00007fff8999198c -[NSDocument _initForURL:withContentsOfURL:ofType:error:] + 151
8 AppKit 0x00007fff89991890 -[NSDocument initForURL:withContentsOfURL:ofType:error:] + 360
9 AppKit 0x00007fff89991677 -[NSDocumentController makeDocumentForURL:withContentsOfURL:ofType:error:] + 199
10 AppKit 0x00007fff8999150f __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_5 + 150
11 AppKit 0x00007fff89991467 __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_4 + 697
12 AppKit 0x00007fff899911a9 -[NSDocumentController _openDocumentWithContentsOfURL:usingProcedure:] + 530
13 AppKit 0x00007fff89990d95 __-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_3 + 242
14 libdispatch.dylib 0x00007fff86329a82 _dispatch_call_block_and_release + 18
15 libdispatch.dylib 0x00007fff8632b8f2 _dispatch_main_queue_callback_4CF + 308
16 CoreFoundation 0x00007fff8bb9be7c __CFRunLoopRun + 1724
17 CoreFoundation 0x00007fff8bb9b486 CFRunLoopRunSpecific + 230
18 HIToolbox 0x00007fff87d652bf RunCurrentEventLoopInMode + 277
19 HIToolbox 0x00007fff87d6c56d ReceiveNextEventCommon + 355
20 HIToolbox 0x00007fff87d6c3fa BlockUntilNextEventMatchingListInMode + 62
21 AppKit 0x00007fff8973d779 _DPSNextEvent + 659
22 AppKit 0x00007fff8973d07d -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
23 AppKit 0x00007fff897399b9 -[NSApplication run] + 470
24 AppKit 0x00007fff899b5eac NSApplicationMain + 867
25 dictionnaireDouchoutique 0x00000001000075d2 main + 34
26 dictionnaireDouchoutique 0x0000000100001984 start + 52
In your NSDocument subclass, override + (BOOL)autosavesDraft and return NO, and you will be safe during your development. However, once you are in production, you should handle the different file formats you have without crashing...
Use NSPropertyListSerialization to deserialize the data, then use respondsToSelector: or isKindOfClass: checks to see what kind of root object it handed you. If it's an array, handle that; if it's a dictionary, handle that; if it's anything you don't recognize, return an error indicating that the file is garbage.
You don't list how you implemented readFromData:, but if you are using NSKeyedUnarchiver you can use containsValueForKey: to validate the objects in the data parameter.
If you want to make old versions incompatible, you can detect old version and populate the error parameter.
Related
In my macOS application I have a very rare crash which I cannot reproduce and I can't find out what causes it.
Please see the call stack below.
Here are my thoughts:
It seems to happen on application start because there's NSPersistentUIRestorer in the call stack.
Then main window view (NSThemeFrame) changes its frame size and sends NSViewGeometryInWindowDidChangeNotification to the children.
Some button tries to setup mouse tracking with _setMouseTrackingForCell and crashes becase some subview (or superview?) is deallocated.
Right now I'm out of ideas and I would appreciate any advice or suggestion. Thank you in advance.
Call stack:
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00006080044e6820
Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x7fff50eb4ea9 objc_msgSend + 41
1 AppKit 0x7fff272c62d5 -[NSView(NSInternal) _uninstallTrackingArea:] + 326
2 AppKit 0x7fff2726525e -[NSView removeTrackingArea:] + 312
3 AppKit 0x7fff2729d3c7 -[NSCell(NSPrivate_CellMouseTracking) _setMouseTrackingInRect:ofView:withConfiguration:] + 101
4 AppKit 0x7fff2729d2fe -[NSCell(NSPrivate_CellMouseTracking) _setMouseTrackingInRect:ofView:] + 90
5 AppKit 0x7fff2729d1bb -[NSButtonCell _setMouseTrackingInRect:ofView:] + 84
6 AppKit 0x7fff2729d15c -[NSControl _setMouseTrackingForCell:] + 102
7 AppKit 0x7fff2729d0af -[NSButton(NSButtonBorder) _setMouseTrackingForCell:] + 47
8 CoreFoundation 0x7fff29ca5edc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
9 CoreFoundation 0x7fff29ca5daa _CFXRegistrationPost + 458
10 CoreFoundation 0x7fff29ca5ae1 ___CFXNotificationPost_block_invoke + 225
11 CoreFoundation 0x7fff29c63880 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1664
12 CoreFoundation 0x7fff29c629b7 _CFXNotificationPost + 599
13 Foundation 0x7fff2bd728c7 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
14 AppKit 0x7fff27ad9859 NSViewHierarchyNoteGeometryInWindowDidChange + 160
15 AppKit 0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
16 AppKit 0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
17 AppKit 0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
18 AppKit 0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
19 AppKit 0x7fff27ad9a3a NSViewHierarchyNoteGeometryInWindowDidChange + 641
20 AppKit 0x7fff2720e9a7 -[NSView _invalidateFocus] + 63
21 AppKit 0x7fff2724e7dc -[NSView setFrameSize:] + 1902
22 AppKit 0x7fff27264b1e -[NSView setFrame:] + 371
23 AppKit 0x7fff2726f49c -[NSThemeFrame _relayoutAuxiliaryViewsOfType:] + 163
24 AppKit 0x7fff2726f3e6 -[NSThemeFrame relayoutAuxiliaryViewsOfType:] + 27
25 AppKit 0x7fff2726ec33 -[NSTitlebarViewController insertChildViewController:atIndex:] + 386
26 AppKit 0x7fff27a88caa -[NSWindowStackController _makeTabBarForWindow:visible:] + 221
27 AppKit 0x7fff27a8bc77 -[NSWindowStackController _addSyncedTabBarItemForWindow:atIndex:] + 625
28 AppKit 0x7fff27a89b96 -[NSWindowStackController insertWindow:atIndex:] + 628
29 AppKit 0x7fff27a8990b -[NSWindowStackController addWindow:] + 437
30 AppKit 0x7fff276180ff -[NSWindow(NSWindowTabbing) _restoreTabbedWindowStateWithCoder:] + 299
31 AppKit 0x7fff27533c82 -[NSWindow restoreStateWithCoder:] + 96
32 AppKit 0x7fff274fcf17 restorePersistentStateWithWindowRestoration + 1004
33 AppKit 0x7fff27533be2 -[NSPersistentUIRestorer invokeRestoration:] + 572
34 AppKit 0x7fff27533947 __79-[NSPersistentUIRestorer finishedRestoringWindowsWithZOrder:completionHandler:]_block_invoke + 194
35 AppKit 0x7fff27533723 +[NSWindow _batchMinimizeWindowsWithBlock:] + 86
36 AppKit 0x7fff274fd475 -[NSPersistentUIRestorer finishedRestoringWindowsWithZOrder:completionHandler:] + 705
37 AppKit 0x7fff27532a79 __82-[NSPersistentUIRestorer restoreStateFromRecords:usingDelegate:completionHandler:]_block_invoke_3 + 168
38 AppKit 0x7fff2753298e __99-[NSApplication(NSPersistentUIRestorationSupport) _restoreWindowWithRestoration:completionHandler:]_block_invoke + 365
39 AppKit 0x7fff27cb94e3 -[NSDocument(NSPersistentUISupport) restoreDocumentWindowWithIdentifier:state:completionHandler:] + 497
40 AppKit 0x7fff2777d3c4 -[NSDocumentControllerPersistentRestoration loadedDocument:forAutoID:] + 176
41 AppKit 0x7fff27782221 __89-[NSDocumentController reopenDocumentForURL:withContentsOfURL:display:completionHandler:]_block_invoke_2 + 185
42 AppKit 0x7fff27790f70 ___NSMainRunLoopPerformBlock_block_invoke + 25
43 CoreFoundation 0x7fff29cae87c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
44 CoreFoundation 0x7fff29c91253 __CFRunLoopDoBlocks + 275
45 CoreFoundation 0x7fff29c91018 __CFRunLoopRun + 3128
46 CoreFoundation 0x7fff29c90153 CFRunLoopRunSpecific + 483
47 HIToolbox 0x7fff28f7ad96 RunCurrentEventLoopInMode + 286
48 HIToolbox 0x7fff28f7ab06 ReceiveNextEventCommon + 613
49 HIToolbox 0x7fff28f7a884 _BlockUntilNextEventMatchingListInModeWithFilter + 64
50 AppKit 0x7fff2722ba73 _DPSNextEvent + 2085
51 AppKit 0x7fff279c1e34 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 3044
52 AppKit 0x7fff27220885 -[NSApplication run] + 764
53 AppKit 0x7fff271efa72 NSApplicationMain + 804
54 MyApp 0x106be1479 main (AppDelegate.swift:17)
55 libdyld.dylib 0x7fff51adc015 start + 1
Update 1:
Running application with zombies shows the following warnings in the output
objc[67272]: Class _NSZombie_CFReadStream is implemented in both ?? (0x6040000bb290) and ?? (0x6040000b8150). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSMachPort is implemented in both ?? (0x604000151990) and ?? (0x6040001532d0). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_CFMachPort is implemented in both ?? (0x604000157110) and ?? (0x60400015c0d0). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteTask is implemented in both ?? (0x604000157250) and ?? (0x604000153a50). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteFileHandle is implemented in both ?? (0x60400016f450) and ?? (0x604000170c90). One of the two will be used. Which one is undefined.
objc[67272]: Class _NSZombie_NSConcreteFileHandle is implemented in both ?? (0x60400016f450) and ?? (0x6040001724d0). One of the two will be used. Which one is undefined.
Update 2:
I managed to reproduce the exact call stack using symbolic break points (but still have never seen the actual crash).
Then using LLDB I requested the information about the button and found out that it is just a regular button with recessed style. Not sure why it causes crash sometimes. May be it's just the first leaf in view hierarchy tree.
For curious ones, here's how I got that information:
(lldb) expr -l Swift -- import Cocoa
(lldb) po $arg1
<NSButton: 0x60f0000ebdb0>
(lldb) expr -l Swift -- let $btn = unsafeBitCast(0x60f0000f7660, to: NSButton.self)
(lldb) expr -l Swift -- print($btn.title)
I have a function:
NSTimer *learnUSBTimer=nil;
- (void)showUSBWindow:(id)sender {
[self updateDeviceButton:#"Cancel"];
[self hideDeviceClearButton:TRUE];
[selectedDeviceField setStringValue:#"Insert the USB device..."];
learnUSBMode=1;
if(learnUSBTimer)
{
if([learnUSBTimer isValid])[learnUSBTimer invalidate];
learnUSBTimer=nil;
}
}
And it seems my message sent to the timer object is being sent to the UI object:
2018-02-19 14:02:34.649581-0500 TokenLock[4068:819373] -[NSBox isValid]: unrecognized selector sent to instance 0x60000016bc40
2018-02-19 14:02:34.649691-0500 TokenLock[4068:819373] [General] -[NSBox isValid]: unrecognized selector sent to instance 0x60000016bc40
2018-02-19 14:02:34.651799-0500 TokenLock[4068:819373] [General] (
0 CoreFoundation 0x00007fff4f33833b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff7607c942 objc_exception_throw + 48
2 CoreFoundation 0x00007fff4f3cf5e4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff4f2aff03 ___forwarding___ + 1443
4 CoreFoundation 0x00007fff4f2af8d8 _CF_forwarding_prep_0 + 120
5 TokenLock 0x0000000100014cdc -[AppController showUSBWindow:] + 252
6 AppKit 0x00007fff4d02375a -[NSApplication(NSResponder) sendAction:to:from:] + 312
7 AppKit 0x00007fff4cac9933 -[NSControl sendAction:to:] + 86
8 AppKit 0x00007fff4cac985b __26-[NSCell _sendActionFrom:]_block_invoke + 136
9 AppKit 0x00007fff4cac9761 -[NSCell _sendActionFrom:] + 183
10 AppKit 0x00007fff4cb0aa18 -[NSButtonCell _sendActionFrom:] + 97
11 AppKit 0x00007fff4cac7fca -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2438
12 AppKit 0x00007fff4cb0a75f -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 777
13 AppKit 0x00007fff4cac6a64 -[NSControl mouseDown:] + 965
14 AppKit 0x00007fff4d1c2959 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5891
15 AppKit 0x00007fff4d1bf5b0 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2359
16 AppKit 0x00007fff4d1be85c -[NSWindow(NSEventRouting) sendEvent:] + 497
17 AppKit 0x00007fff4d01f617 -[NSApplication(NSEvent) sendEvent:] + 307
18 AppKit 0x00007fff4c880d9d -[NSApplication run] + 812
19 AppKit 0x00007fff4c84ff1a NSApplicationMain + 804
20 TokenLock 0x0000000100001ea1 main + 97
21 TokenLock 0x0000000100001e34 start + 52
)
I dont understand how my NSTimer is being somehow assigned to the UI Box being clicked to execute the function?
It seems that there will be some memory management issue in your code. And the objects are not properly allocated/ deallocated or not initialised deinitialised properly.
So to solve this issue just run your code by enabling zombies into your project target settings.
It turns out this timer fires once. I was not setting it to nil in that code and so it became essentially an orphan because the system released the actual timer object after.
Setting it to nil on firing fixes the issue.
I have 3 NSToolbarItems and the first one works just fine, the other two, each time I select them throw
[16392:513506] -[NSToolbarItem isEqualToString:]: unrecognized selector sent to instance 0x6080001203c0
2015-05-26 19:59:26.871 MyApp[16392:513506] (
0 CoreFoundation 0x00007fff89f9703c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8a40176e objc_exception_throw + 43
2 CoreFoundation 0x00007fff89f9a0ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x00007fff89edfe24 ___forwarding___ + 1028
4 CoreFoundation 0x00007fff89edf998 _CF_forwarding_prep_0 + 120
5 AppKit 0x00007fff9649d74e +[NSToolbar _shouldIgnoreItemIdentifier:] + 35
6 AppKit 0x00007fff9649d6f7 removeIgnoredIdentifiers + 89
7 AppKit 0x00007fff9675e526 -[NSToolbar selectableItemIdentifiers] + 224
8 AppKit 0x00007fff9675e411 -[NSToolbar _isSelectableItemIdentifier:] + 34
9 AppKit 0x00007fff9675e385 -[NSToolbar setSelectedItemIdentifier:] + 79
10 AppKit 0x00007fff96654f3c -[NSToolbarItemViewer mouseDown:] + 4951
11 AppKit 0x00007fff96bc52fc -[NSWindow _reallySendEvent:isDelayedEvent:] + 14125
12 AppKit 0x00007fff96554d76 -[NSWindow sendEvent:] + 470
13 AppKit 0x00007fff96551312 -[NSApplication sendEvent:] + 2504
14 AppKit 0x00007fff9647ac68 -[NSApplication run] + 711
15 AppKit 0x00007fff963f7354 NSApplicationMain + 1832
16 IPLocation 0x000000010001769d main + 109
17 libdyld.dylib 0x00007fff96ffd5c9 start + 1
18 ??? 0x0000000000000003 0x0 + 3
)
I tried disconnecting and reconnecting them from the storyboard,but nothing changed. The IBAction doesn't even get called.
My ViewController
func toolbarSelectableItemIdentifiers(toolbar: NSToolbar) -> [AnyObject] {
// let array = toolbar.items.filter { contains(Identifier.allValues, $0.itemIdentifier ) }//wrong!
return Identifier.allValues
}
The delegate just needed the name of the identifiers, not the selectable items themselves.
I have a subclass of NSDocument that crashes whenever I use [self close] to avoid the default 'save' dialog for new documents:
2013-02-25 15:23:06.338 MyApp[692:403] An uncaught exception was raised
2013-02-25 15:23:06.338 MyApp[692:403] NSWindow: -_newFirstResponderAfterResigining is not a valid message outside of a responder's implementation of -resignFirstResponder.
2013-02-25 15:23:06.356 MyApp[692:403] (
0 CoreFoundation 0x00007fff8b2cdf56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff88c00d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8b2cdd8a +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff8b2cdd14 +[NSException raise:format:] + 116
4 AppKit 0x00007fff858e0c34 -[NSWindow _newFirstResponderAfterResigning] + 164
5 AppKit 0x00007fff85882c8a -[NSTextView(NSSharing) resignFirstResponder] + 54
6 AppKit 0x00007fff8536dd79 -[NSWindow makeFirstResponder:] + 429
7 AppKit 0x00007fff85488dfb -[NSWindow dealloc] + 802
8 AppKit 0x00007fff8528b955 -[NSWindow release] + 535
9 AppKit 0x00007fff858b541c __destroy_helper_block_4 + 26
10 libsystem_blocks.dylib 0x00007fff8702b174 _Block_release + 181
11 libdispatch.dylib 0x00007fff84ab28f2 _dispatch_main_queue_callback_4CF + 308
12 CoreFoundation 0x00007fff8b262e7c __CFRunLoopRun + 1724
13 CoreFoundation 0x00007fff8b262486 CFRunLoopRunSpecific + 230
14 HIToolbox 0x00007fff8d64e2bf RunCurrentEventLoopInMode + 277
15 HIToolbox 0x00007fff8d65556d ReceiveNextEventCommon + 355
16 HIToolbox 0x00007fff8d6553fa BlockUntilNextEventMatchingListInMode + 62
17 AppKit 0x00007fff85252779 _DPSNextEvent + 659
18 AppKit 0x00007fff8525207d -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135
19 AppKit 0x00007fff8524e9b9 -[NSApplication run] + 470
20 AppKit 0x00007fff854caeac NSApplicationMain + 867
21 MyApp 0x00000001059098d2 main + 34
22 MyApp 0x00000001059098a4 start + 52
23 ??? 0x0000000000000003 0x0 + 3
)
Has anyone seen this before or can anyone suggest a better/proper way to close an NSDocument without the save dialog being shown?
So judging by your followup comments, what you're trying to do is suppress the standard close confirmation sheet under specific circumstances. Some ways to do that:
Adopt autosaving-in-place, which moves away from the ancient paradigm of manually saving documents
Override -[NSDocument canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:] so it tells the delegate "yes, you can close", rather than presenting a sheet
Once a post is made, call [document updateChangeCount: NSChangeCleared] to inform the document that you consider it to be saved
I'm new to Cocoa programming under OSX Lion, so this is a question I cannot find answer to.
I need to modify an attributed string which is entered by the user through the NSTextView.
Editing may include replacement of some portion(s) of the string inside the NSTextStorage and addition of NSTextAttachment objects.
I implemented the protocol and provided it for NSTextView instance as delegate.
I also implemented the method textView:shouldChangeTextInRanges:replacementStrings: in the next manner:
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRanges:(NSArray *)affectedRanges replacementStrings:(NSArray *)replacementStrings{
NSTextStorage* ts=[textView textStorage];
[ts beginEditing];
NSMutableString* buf=[ts mutableString];
//make some completions
NSRange rng;
rng.location=0;
rng.length=[buf length];
[buf replaceOccurrencesOfString:#"(." withString:#"(0." options:0 range:rng];
rng.location=0;
rng.length=[buf length];
[buf replaceOccurrencesOfString:#"-+" withString:#"-" options:0 range:rng];
rng.location=0;
rng.length=[buf length];
[buf replaceOccurrencesOfString:#"+-" withString:#"-" options:0 range:rng];
[ts endEditing];
return YES;
}
Thus I try the next scenario: when the application is up, the user types inside the NSTextView some text. If he types "(." everything is going well. But when he types "-+ " (minus, plus, whitespace) - the method crashes producing this dump:
[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
(
0 CoreFoundation 0x00007fff95963286 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff8c32bd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff959630ba +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff95963044 +[NSException raise:format:] + 116
4 AppKit 0x00007fff90acb5bd -[NSConcreteTextStorage attributesAtIndex:effectiveRange:] + 121
5 AppKit 0x00007fff90f4cde6 -[NSTextView insertText:replacementRange:] + 840
6 AppKit 0x00007fff910fc891 -[NSTextInputContext handleTSMEvent:] + 2289
7 AppKit 0x00007fff910fe4fe _NSTSMEventHandler + 165
8 HIToolbox 0x00007fff89745308 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1263
9 HIToolbox 0x00007fff89744914 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 446
10 HIToolbox 0x00007fff8975b6c7 SendEventToEventTarget + 76
11 HIToolbox 0x00007fff89930a4d SendTSMEvent + 35
12 HIToolbox 0x00007fff89930dd4 SendUnicodeTextAEToUnicodeDoc + 452
13 HIToolbox 0x00007fff8993095e TSMKeyEvent + 629
14 HIToolbox 0x00007fff8976f81a TSMProcessRawKeyEvent + 2350
15 AppKit 0x00007fff910fbade -[NSTextInputContext handleEvent:] + 639
16 AppKit 0x00007fff90fc7eaf -[NSView interpretKeyEvents:] + 248
17 AppKit 0x00007fff90f40c65 -[NSTextView keyDown:] + 691
18 AppKit 0x00007fff90a20544 -[NSWindow sendEvent:] + 7430
19 AppKit 0x00007fff909b868f -[NSApplication sendEvent:] + 5593
20 AppKit 0x00007fff9094e682 -[NSApplication run] + 555
21 AppKit 0x00007fff90bcd80c NSApplicationMain + 867
22 myapp 0x0000000100001302 main + 34
23 myapp 0x00000001000012d4 start + 52
It seems that shortened string causes some out-of-bounds spill.
What am I doing wrong and how can I fix this issue? Please help.
The -length method returns the quantity of characters in a NSString. The problem is that the first character is in the index 0. Try using [buf length] - 1. This should work.