简体   繁体   中英

Objective-C NSString Issue

I have global stings as such:

NSString *test = nil;
NSString *test1 = nil;

Do I need to alloc with anything? I do change the String value constantly throughout the program, and i'm getting a EXC_BAD_ACCESS error with segmented control that kills the program.

Any suggestions?

*** Call stack at first throw:
(
    0   CoreFoundation                      0x00db6be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f0b5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00db86fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d28366 ___forwarding___ + 966
    4   CoreFoundation                      0x00d27f22 _CF_forwarding_prep_0 + 50
    5   tipApp                              0x00005f69 -[tipAppViewController exact] + 54
    6   tipApp                              0x00004907 -[tipAppViewController segmentedControlIndexChanged] + 129
    7   UIKit                               0x002bfa6e -[UIApplication sendAction:to:from:forEvent:] + 119
    8   UIKit                               0x0034e1b5 -[UIControl sendAction:to:forEvent:] + 67
    9   UIKit                               0x00350647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    10  UIKit                               0x0034e16c -[UIControl sendActionsForControlEvents:] + 49
    11  UIKit                               0x0038d6b2 -[UISegmentedControl setSelectedSegmentIndex:] + 574
    12  UIKit                               0x0039217e -[UISegmentedControl touchesBegan:withEvent:] + 971
    13  UIKit                               0x002e4025 -[UIWindow _sendTouchesForEvent:] + 395
    14  UIKit                               0x002c537a -[UIApplication sendEvent:] + 447
    15  UIKit                               0x002ca732 _UIApplicationHandleEvent + 7576
    16  GraphicsServices                    0x016eca36 PurpleEventCallback + 1550
    17  CoreFoundation                      0x00d98064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    18  CoreFoundation                      0x00cf86f7 __CFRunLoopDoSource1 + 215
    19  CoreFoundation                      0x00cf5983 __CFRunLoopRun + 979
    20  CoreFoundation                      0x00cf5240 CFRunLoopRunSpecific + 208
    21  CoreFoundation                      0x00cf5161 CFRunLoopRunInMode + 97
    22  GraphicsServices                    0x016eb268 GSEventRunModal + 217
    23  GraphicsServices                    0x016eb32d GSEventRun + 115
    24  UIKit                               0x002ce42e UIApplicationMain + 1160
    25  tipApp                              0x00001a8c main + 102
    26  tipApp                              0x00001a1d start + 53
)
terminate called after throwing an instance of 'NSException'
  • Note this only happens when I start the program out, all strings are nil, and I hit all buttons on the segemented control

I'm just not sure exactly what is going on here

You have to retain that strings and release every time you reassign new value. It doesn't matter whether it is static variable or ivar. The same memory rules are applied:

[test release];
test = [@"new_value" retain];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM