1

I am getting an abrupt crash on a initWithFormat:arguments command; I suspect it depends on some memory issue as the crash is quite unreliable; I had this code since months and just today it started crashing! Is there some evident issue in my code leading to dangling references, I am not very familar with C structures in objective-c.

- (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments{
if ([arguments count]==0) return format;
NSLog(@"argument counts %lu", (unsigned long)[arguments count]);
NSRange range = NSMakeRange(0, [arguments count]);
NSMutableData* data = [NSMutableData dataWithLength:sizeof(id) * [arguments count]];
[arguments getObjects:(__unsafe_unretained id *)data.mutableBytes range:range];
NSLog(@"format %@ arguments=%@", format, arguments);
NSString* result = [[NSString alloc] initWithFormat:format arguments:data.mutableBytes];
return result;
}

Format and parameters match, as I could see in the NSLog. This is the crash log:

Thread 0 Crashed: 0 libobjc.A.dylib 0x0000000102152fc5 0x10213a000 + 102341 1 Foundation 0x0000000101ad9137 0x101a99000 + 262455 2 CoreFoundation
0x0000000105278244 0x105273000 + 21060 3 CoreFoundation
0x00000001052b6913 0x105273000 + 276755 4 Foundation
0x0000000101ad80b2 0x101a99000 + 258226 5 inArrivoHD
0x00000001000041ea 0x100000000 + 16874 6 inArrivoHD
0x0000000100004445 0x100000000 + 17477 7 inArrivoHD
0x00000001000069f0 0x100000000 + 27120 8 inArrivoHD
0x0000000100006ef0 0x100000000 + 28400 9 libdispatch.dylib
0x000000010570c851 0x10570b000 + 6225 10 libdispatch.dylib
0x000000010571f72d 0x10570b000 + 83757 11 libdispatch.dylib
0x000000010570f3fc 0x10570b000 + 17404 12 CoreFoundation
0x0000000105351289 0x105273000 + 909961 13 CoreFoundation
0x000000010529e854 0x105273000 + 178260 14 CoreFoundation
0x000000010529dd83 0x105273000 + 175491 15 GraphicsServices
0x000000010236af04 0x102360000 + 44804 16 UIKit
0x0000000100ab3e33 0x100a9f000 + 85555 17 inArrivoHD
0x00000001000013a3 0x100000000 + 5027 18 libdyld.dylib
0x00000001059705fd 0x10596d000 + 13821

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • Pls add the stack trace, add some info about the context in which you are using that code and tell us the operations that you did to understand the problem (breakpoints etc) or is impossible to understand – Andrea May 19 '14 at 09:07
  • I added the crash log in the question: it is not symbolicated as I could not do it any longer since I installed the crash reporter. – Fabrizio Bartolomucci May 19 '14 at 09:11
  • You want to create string from NSArray? If so then maybe this question can help you: http://stackoverflow.com/questions/2386834/how-to-convert-nsarray-into-nsstring – Josip B. May 19 '14 at 09:24
  • Interesting, that would get rid of the C memory management issues! In that case I think I could use the normal string formatter, too. I will check it tonight and let you know. For reference the suggestion for my own case I think would be to use: NSString* params=[arguments componentsJoinedByString: @","]; NSString* result =[NSString stringWithFormat:format, params]; or alternatively: NSString *params= [arguments description]; I will check both tonight. Thanks for now, someone should tell people proposing more intricate and dangerous solutions to remove them. – Fabrizio Bartolomucci May 19 '14 at 11:32
  • Unfortunately it crashes too. The strange thing is that the parameters come without the quotes when I log them, as in: params=San Francisco,In Arrivo HD when it should of course be "San Francisco", "In Arrivo HD". – Fabrizio Bartolomucci May 19 '14 at 12:29
  • Yet, when I use: NSString *params= [arguments description]; it does not crash any longer, but I am not sure about what it does as I am connecting by afar through my iPad and iTeleport and it is not so easy to check what happens. I will let you know ass soon as I may check it. – Fabrizio Bartolomucci May 19 '14 at 12:38
  • Nothing: this latter does not crash only because it stuffs the whole array into the first parameter and puts null in the second. Not exactly what I need. Even in this array, though, while 'In Arrivo HD' is inside quotes, location Woodside is without them, even if it is a standard NSString property, and I totally ignore what type could it be instead. I think it is source of the crash in all implementations, but I totally ignore what has happened. This is the format of the Array (after having had the description of it) in the logs: ( Woodside, "In Arrivo HD") – Fabrizio Bartolomucci May 19 '14 at 12:47
  • I found this function that at least does not crash, but that seems to only work with a single parameter: + (id)stringWithFormat:(NSString *)format array:(NSArray*) arguments; { __unsafe_unretained id * argList = (__unsafe_unretained id *) calloc(1UL, sizeof(id) * arguments.count); for (NSInteger i = 0; i < arguments.count; i++) { argList[i] = arguments[i]; } NSString* result = [[NSString alloc] initWithFormat:format, *argList] ;// arguments:(void *) argList]; free (argList); return result; } Do you know of any fix to have it fully functional? – Fabrizio Bartolomucci May 19 '14 at 20:51

1 Answers1

0

Finally I came our with this silly function that at least works:

+ (NSString *)stringWithFormat:(NSString *)format arguments:(NSArray *) arguments
{
    return [NSString stringWithFormat:format,
      (arguments.count > 0) ? arguments[0]: nil,
      (arguments.count > 1) ? arguments[1]: nil,
      (arguments.count > 2) ? arguments[2]: nil,
      ...
      (arguments.count > 20) ? arguments[20]: nil];
}
kpower
  • 3,871
  • 4
  • 42
  • 62
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75