0

If this issue has been posted before, I can't find it.

I'm attempting to allocate and initialize an instance of NSString in the initialization method of a subclass of NSOperation (for use with NSOperationQueue). The NSString pointer is an ivar (not a property).

The program crashes with "EXC_BAD_ACCESS (code=EXC_I386_GPFLT)".

To isolate the problem, I've separated the alloc and init functions. The main thread is crashing on:

m_myString = [NSString alloc];

The code is in an "if" block:

if (someCondition)
{
  m_myString = [NSString alloc];
  m_myString = [m_myString initWithCString:aCharPointer encoding:NSASCIIStringEncoding];
}
else
{
  m_myString = [NSString alloc];
  m_myString = [m_myString initWitCString:aDifferentCharPointer encoding:NSASCIIStringEncoding];
}

Examining the thread shows that it's crashing on objc_release. I don't understand why release would be called on an object in the allocation method, but that seems to be the case...

It's worth mentioning that I successfully alloc and init another instance variable NSString in the same method before the if block.

Has anyone else run into this before, and if so, how'd you solve it?

I'd be glad to give more info upon request.

SumDood
  • 294
  • 1
  • 7
  • 1
    `objc_release` could be called on the *previous* reference stored in `m_myString`, have you looked at that? – CRD Oct 31 '15 at 05:18
  • Interesting thought, but I'm 99% sure that's not the case, since this is occurring in the initialization function. This instance of NSString hasn't ever stored a value previously, and calling alloc to set the pointer is actually the first time I even access the pointer. – SumDood Oct 31 '15 at 15:06
  • @trojanfoe found [this question](http://stackoverflow.com/questions/7498388/why-in-objective-c-does-doing-alloc-and-init-in-separate-statements-cause-the-ob) as a possible duplicate. What that tells you is that your code change while tracking down the problem may also have introduced a `release`, I.e. you may have inadvertently obscure the issue. – CRD Oct 31 '15 at 17:36
  • @CRD Huh? how am involved, other than closing this question as duplicate earlier? – trojanfoe Oct 31 '15 at 17:38
  • @trojanfoe - just giving you credit for finding the other question, it's relevant but I don't think it's a dupe. – CRD Oct 31 '15 at 17:42
  • @CRD Yeah maybe. The issue is so easy to avoid with `m_myString = someCondition ? @(cCharPointer) : @(aDifferentCharPointer);`, however... – trojanfoe Oct 31 '15 at 17:44
  • This is a simplification. there's actually several nested if / else if / else statements, and m_myString takes on a different value depending on the state of the software. Additionally, it crashes in objc_release if I pair alloc and init together, and even if I call a class method of NSString that returns an object I don't own, such as `[NSString stringWithCString: encoding:];` It even crashes if I simply assign it to a string literal, like `m_myString = @"test";` Out of curiosity, why would using `m_myString = someCondition ? @(charPointer) : @(diffCharPointer); make any difference? – SumDood Oct 31 '15 at 19:21
  • @LiftAndMath - The reason we've given you suggestions for things to look at is because there is not enough info here to provide an *answer*. That assigning a literal causes the same issue does point to the variable itself, and then the object it is part of, etc... You're going to have to dig further and/or generate a smaller complete sample with the error that you can post so others can try to reproduce it. There is nothing wrong *per se* with your original code, and breaking the `alloc` and `init` up as you later did, while not recommended, shouldn't cause a problem either. Good hunting! – CRD Oct 31 '15 at 19:35

0 Answers0