0

Recently I got a few EXC_BAD_ACCESS crashes from firebase when calling [NSString stringWithFormat:]. The code snipped is as follows:

#define TYPE_A @"typeA"
#define TYPE_B @"typeB"    

- (void)myMethod {
    NSString *type;
    if (condition1) {
        type = TYPE_A;
    } else if (condition2) {
        type = TYPE_B;
    }
    if (type) {
        NSString *message = [NSString stringWithFormat:@"Message: %@", type];
    }
}

This snippet is very simple so the only cause I can guess is that when type is declared, it is assigned a garbage value.

However, I found out that someone claimed years ago that under ARC, object pointers like this will be set to nil.(See Link1 Link2) If this is true, maybe there's a bug in LLVM; if not, what caused this crash?

P. Tsin
  • 435
  • 4
  • 14
  • 1
    I must have 100,000 lines of code that don’t set an object to nil because official documentation from Apple assures us that clang does it. I don’t ever get a crash that implies some random value (ie it was never set to nil). There is something else going on. – David H Jul 30 '22 at 11:16
  • Can you share samples of these crash logs, and the backtraces associated with them? Plenty of other things can be going wrong here, including extremely deep call stacks and memory corruption (but the best way to find out would be with a symbolized backtrace). – Itai Ferber Jul 30 '22 at 15:11
  • @DavidH How about primitive type variables, like an uninitialized char array? Does clang also make sure every char is set to NULL? – P. Tsin Jul 31 '22 at 04:16
  • Instance variables in classes are all set to zeros unlike C++. But primitive variables in functions or methods are not initialized. When a class object is allocated (alloc), all memory is set to zero. – David H Jul 31 '22 at 11:28
  • @DavidH : in this case the var is in a method so it is not initialised , right ? – Ptit Xav Jul 31 '22 at 15:35
  • @PtitXav in the original code above, type is set to nil. If there was a second variable, say “int i;”, then “i” is not initialized, so it’s some random value. – David H Aug 02 '22 at 11:11
  • @DavidH I just found the code I'm working on is actually based on one of your answers a few years ago. See: https://stackoverflow.com/questions/7072989/how-to-get-my-ip-address-programmatically-on-ios-macos/10803584#10803584 . In **getIPAddresses** method, *addrBuf* may contain random value when it's declared, correct? – P. Tsin Aug 03 '22 at 02:56
  • @P.Tsin exactly - the data is total gibberish - random bytes. – David H Aug 03 '22 at 10:05

0 Answers0