NSMutableData appendBytes is setting length to -1 and crashing - ios

I'm getting a rare and intermittent crash which looks like appendBytes being called with -1 as it's length.
now, I've hard coded the "length" argument every time I've used this method so I can't see how this could happen and worse still I can't see how I could test for and avoid this crash.
here's the top of the stack and the exception (note the ~4.2b length):
*** Terminating app due to uncaught exception 'NSMallocException', reason: '*** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (4294967295)'
*** Call stack at first throw:
(
0 CoreFoundation 0x91ec5a67 __raiseError + 231
1 libobjc.A.dylib 0x9950a149 objc_exception_throw + 155
2 CoreFoundation 0x91e2d289 +[NSException raise:format:arguments:] + 137
3 CoreFoundation 0x91e2d1f9 +[NSException raise:format:] + 57
4 Foundation 0x92d2489e _NSMutableDataGrowBytes + 1136
5 Foundation 0x92d24391 -[NSConcreteMutableData appendBytes:length:] + 354
here's a simplified version of the code that's supposedly crashing:
if (self.isConnectedToService) {
NSMutableData *myData = [NSMutableData data];
float newValue = PanValue;
const char theTwo[] = {(char)Chan_L, (char)PanParam};
[myData appendBytes:&theTwo length:2];
[myData appendBytes:&newValue length:4];
}
So length is always 2 or 4.
I've tested different situations in which the buffers contain more or less than 2 and 4 and I've never managed to cause this crash intentionally.
I've got the same code running on both MacOS10.7.4 and iOS6.0(on iPad3) and see this issue occasionally on both platforms.
so how is appendBytes getting that bogus value?

Related

Irregular crash when creating NSManagedObject out of NSSet

I have an irregular crash (1 in 5 times on devices, 4 in 5 times on Simulator). I have set an exception breakpoint and it occurs on the following line without any console information:
if let carColorSet = car.carToDisplay?.allObjects as? [Display] {
Note: car.carToDisplay is an NSSet
Before I set the exception breakpoint, I would get the following info in the console when it crashed:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableSet unionSet:]: set argument is not an NSSet'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102e79b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001023d6141 objc_exception_throw + 48
2 CoreFoundation 0x0000000102e00051 -[NSMutableSet unionSet:] + 1041
3 CoreData 0x0000000101b10df8 -[_NSFaultingMutableSet willReadWithContents:] + 936
4 CoreData 0x0000000101af131b -[_NSFaultingMutableSet allObjects] + 27
5 Keyboard 0x00000001018f838e
Any idea here? It seems like the crash occurs when I try to turn that carToDisplay?.allObjects into an array of Display objects.
This is a keyboard extension app btw.
Thank you for any input!
Problem (probably):
if let carColorSet = car.carToDisplay?.allObjects as? [Display]
carToDisplay is a NSet
However the function allObjects returns an array
I think you are trying to do a unionSet with an array.
Update the Question:
Please update your question, the qestion doesn't state the correct line of error.
The question needs to do the following
state where the unionSet is invoked.
what the argument type of unionSet is.

How to map a local JSON string into an object using RestKit 0.24 with RKObjectMapping?

I am trying to map a JSON NSString into an object using RestKit 0.24.0 where I have a RKObjectMapping as well as the corresponding dictionary.
Most of the solutions online refer to RestKit 0.22 and below.
Some of the solutions on SO result into app crash.
What is the easiest way to convert a local string into an object. Consider the following
CODE UPDATE
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:parsedData mappingsDictionary:[RJobObject mappingDictionary]];
RJobObject * rrrr = [[RJobObject alloc] init];
mapper.targetObject = rrrr;
//mapper.mappingOperationDataSource = parsedData;
[mapper execute:nil];
Here , mappingDictionary is basically the key-value pair matching between the JSON key and Object variable name
So in the above code whenever I run execute, the app crashes.
Stack Trace
2015-11-22 09:12:29.193 Help[22427:699579] -[__NSCFConstantString forceCollectionMapping]: unrecognized selector sent to instance 0x1022736a0
2015-11-22 09:12:29.211 Help[22427:699579] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString forceCollectionMapping]: unrecognized selector sent to instance 0x1022736a0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000105d7dc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105a14bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000105d850ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000105cdb13c ___forwarding___ + 988
4 CoreFoundation 0x0000000105cdacd8 _CF_forwarding_prep_0 + 120
5 Help 0x000000010212f424 -[RKMapperOperation mapRepresentationOrRepresentations:atKeyPath:usingMapping:] + 132
6 Help 0x000000010212fe97 -[RKMapperOperation mapSourceRepresentationWithMappingsDictionary:] + 1959
7 Help 0x00000001021307a2 -[RKMapperOperation main] + 1330
8 Foundation 0x00000001034c4774 -[__NSOperationInternal _start:] + 645
9 Help 0x00000001021310b7 -[RKMapperOperation execute:] + 39
10 Help 0x0000000101f62f8e -[AppDelegate fetchJobsFromDB] + 1102
More Details
Updated to RestKit 0.26.0 , am still facing the issue.
Basically in RKMapperOperation.m, line 333 a po mapping.forceCollectionMapping gives an error and it crashed the app.
ANSWER : Sample Solution
Use RKMappingOperation , as we do not have to specify the keyPath
RJobObject * rrrJob = [RJobObject new];
RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:jobEntity.dictionary destinationObject:rrrJob mapping:[RJobObject mapping]];
mappingOperation.dataSource = (id)rrrJob;
[mappingOperation start];
For what you're trying to do 'parsedData' should be a dictionary or array that has been deserialised from the JSON string, and [RJobObject mappingDictionary] should be a dictionary where the keys are the source key paths and the values are RKMapping Instances defining what to do with the contents.
The idea is that the key paths determine how to drill into the JSON data and the mapping determines how to process the content found there. Your key path might be an empty string if you want to process the whole JSON data.
You might consider using RKMappingOperation instead if you want to avoid the key path complexity and you're mapping the whole JSON into a single target object.

I cannot catch the "No space left on device" exception when using NSFileHandle writedata function

I used NSFileHandle to writedata to a file.
NSFilehandle *handle = [NSFileHandle fileHandleForWritingAtPath:#"path/of/file"];
#try {
[handle writedata:data];
} #catch (NSException *e) {
// when exception occur, never got here
NSLog(#"%#", e);
}
As my device disk space is full, calling writedata will fail. But I can't catch the exception.
My program log info:
2014-05-23 16:17:24.435 mytest[12919:3203] An uncaught exception was raised
2014-05-23 16:17:24.435 mytest[12919:3203] *** -[NSConcreteFileHandle writeData:]:
No space left on device
2014-05-23 16:17:24.436 mytest[12919:3203] (
0 CoreFoundation 0x00007fff8fae725c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8cbb3e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8fae710c +[NSException raise:format:] + 204
3 Foundation 0x00007fff93640f31 __34-[NSConcreteFileHandle writeData:]_block_invoke + 84
4 Foundation 0x00007fff93840020 __49-[_NSDispatchData enumerateByteRangesUsingBlock:]_block_invoke + 32
5 libdispatch.dylib 0x00007fff8bfc2fad _dispatch_client_callout3 + 9
6 libdispatch.dylib 0x00007fff8bfc2f28 _dispatch_data_apply + 113
7 libdispatch.dylib 0x00007fff8bfc9502 dispatch_data_apply + 31
8 Foundation 0x00007fff9383fff9 -[_NSDispatchData enumerateByteRangesUsingBlock:] + 83
9 Foundation 0x00007fff93640ed2 -[NSConcreteFileHandle writeData:] + 150
10 ZiSyncMac 0x000000010000b1eb -[TransferFile writePart:data:] + 475
I found out writedata calls a dispatch to do real write data to the file system. So I think writedata will throw a exception in GCD.
How can I handle the GCD exception in my code?
From a website that's been hijacked since (not providing the link for that reason, but the explanation is still useful):
seekToEndOfFile() and writeData() are not marked as throws (they don’t
throw an NSError object which can be caught in with a do-try-catch
block), which means in the current state of Swift, the NSExceptions
raised by them cannot be “caught”.
If you’re working on a Swift project, you could create an Objective-C
class which implements your NSFileHandle methods that catches the
NSExceptions (like in this question), but otherwise you’re out of
luck.
What #eric said is right.
But, what you can do is check before that you write for how much space is left in the disk and only after it write to the disk.

Xcode NSSortDescriptor Error Thread 1: signal ABRT

Hi I'm creating a stocks application in the Objective-C Big Nerd Ranch 2nd edition book and at the end of each chapter theres a challenge and the chapter i'm on has a challenge which is add a method to BNRPortfolio that returns an NSArray of all its stock holdings, sorted alphabetically by symbol and then test it in main(). Everything works great except for this method(which is declared in the BNRPortfolio.h). It gives me this error: Thread 1: signal ABRT. And prints the message below in the console. The error report and what I think it is, is there is something happening the the sortDescriptorWithKey:#"symbol" part but the error appears on the [sortedBySymbolHoldings sortUsingDescriptors:#[s]]; part which is whats throwing me off. If you have any ideas on how to fix this your help is greatly appreciated thanks!
Code in BNRPortfolio.m:
- (NSArray *)holdingsSortedBySymbol
{
NSSortDescriptor *s = [NSSortDescriptor sortDescriptorWithKey:#"symbol" ascending:YES];
NSMutableArray *sortedBySymbolHoldings = [[NSMutableArray alloc] init];
sortedBySymbolHoldings = _stocks;
[sortedBySymbolHoldings sortUsingDescriptors:#[s]];
return sortedBySymbolHoldings;
}
What it prints in the console:
2014-02-06 16:18:26.309 Stocks[1286:303] Portfolio for UK has a value of £279.50
2014-02-06 16:18:26.311 Stocks[1286:303] Portfolio for US has a value of $12772.00
2014-02-06 16:18:26.311 Stocks[1286:303] Top 3 most valuable holdings: (
"<BNRStockHolding: 0x100106e90>",
"<BNRStockHolding: 0x100102f70>",
"<BNRStockHolding: 0x100104c90>"
)
2014-02-06 16:18:26.312 Stocks[1286:303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<BNRStockHolding 0x100106e90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key symbol.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff85d5a41c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8d933e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff85d59fc9 -[NSException raise] + 9
3 Foundation 0x00007fff8377e06e -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 235
4 Foundation 0x00007fff83695406 -[NSObject(NSKeyValueCoding) valueForKey:] + 381
5 Foundation 0x00007fff836ad49f -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 334
6 Foundation 0x00007fff836ac04c _sortedObjectsUsingDescriptors + 394
7 Foundation 0x00007fff8370fbd6 -[NSMutableArray(NSKeyValueSorting) sortUsingDescriptors:] + 466
8 Stocks 0x0000000100002018 -[BNRPortfolio holdingsSortedBySymbol] + 264
9 Stocks 0x00000001000028c2 main + 1522
10 libdyld.dylib 0x00007fff84b005fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

NSArray exception when adding track to starred playlist

Sometimes, I'm getting a fatal exception when trying to "star" a track in cocoalibspotify. I'm logging in with a user with a heavy dataset (hundreds of playlists, but < 15 starred tracks).
This is how I "star" the SPTrack:
[[[[SPSession sharedSession] starredPlaylist] items] addObject:myTrack];
... and the stack trace:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray objectsAtIndexes:]: index 12 beyond bounds [0 .. 11]'
*** Call stack at first throw:
(
0 CoreFoundation 0x01e125a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x021a1313 objc_exception_throw + 44
2 CoreFoundation 0x01da0f99 -[NSArray objectsAtIndexes:] + 633
3 Foundation 0x016a250b -[NSKeyValueArray objectsAtIndexes:] + 110
4 Foundation 0x016aaca6 NSKeyValueDidChangeByArrayMutation + 103
5 Foundation 0x01610c30 NSKeyValueDidChange + 266
6 Foundation 0x016aba95 -[NSObject(NSKeyValueObserverNotification) didChange:valuesAtIndexes:forKey:] + 123
7 Foundation 0x016a4d0e -[NSKeyValueNotifyingMutableArray addObject:] + 239
8 MyApp 0x000922cd -[PlaylistManager starTrack:] + 285
...
This only seems to happen within a minute or two after logging in (i.e. user data is loaded), so I'm guessing it might be an issue where the data isn't fully loaded or something?
I've tried to find out if there's any properties to observe in order to find out when everything's fully loaded. But as the array might be empty (and the user might not have starred any tracks before), it seems like there's no good way of validating that everything's loaded...? The loaded property of SPPlaylist seems to refer to the playlist metadata, and not its tracks (?).
Any ideas?
You are getting the So it looks like you're not supposed to manipulate that array directly. Instead call [myTrack setStarred:YES];
Basically, I just went through the entire Spotify API for you to find that answer.
A full code sample for this:
-(void)starTrack:(NSString *)uri {
NSURL *trackURL = [NSURL URLWithString:uri];
[[SPSession sharedSession] trackForURL:trackURL callback:^(SPTrack *track) {
if (track != nil) {
[SPAsyncLoading waitUntilLoaded:track timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *tracks, NSArray *notLoadedTracks) {
[track setStarred:YES];
}];
}
}];
}

Resources