Search code examples
objective-cxcodemacosnsstringibaction

Issue with NSString access from IBAction in a macOS app for 10.4 Tiger PPC in Xcode 2.5


So this is a rather obscure issue but here goes. I have access to only 1 kind of Mac at work (I teach programming at a high school) and that's an old EMac 1.25Ghz. I've got it running Tiger. I also have a 1Ghz EMac at home also running Tiger, both have Xcode 2.5 installed.

My app works fine for the most part but I find I am having issues writing to NSString variables from IBActions. I can read the strings fine and read and write to ints or bools, but NSStrings throw a BAD_EXEC_ACCESS error whenever I try to write to them if they are variables that exist outside the IBAction method. The code works fine on my Mac Pro at home, compiled for macOS 14, but not in Tiger, or Leopard. I've tried recompiling the app in Xcode 3.1 but get the same issues. I'm looking for a vet or 2 who can tell me how to correctly set up NSStrings to be accessed from the IBAction.

Here's my code:

#import "GameWindow.h"

@implementation GameWindow

// game variables
int gameStage;
int playerType;
int startUpSetting;
bool languageSelect;
int playerPortrait;
bool firstTimeGettingInput;

//strings are char characters

NSString *playerName;
NSString *inputText;
NSString *onGoingDisplayText;


-(void)awakeFromNib{ //init the view here, set text, ect

    
    //define variable values
    
    gameStage = 0; //where we are in the game
    playerType = 0; //who we're playing as
    startUpSetting = 1; //this becomes 0 once start up perameters have been set
    languageSelect = 0; //0 is English, 1 is Japanese
    playerPortrait = 4; //what the players portrait looks like.
    firstTimeGettingInput = true; //has the game just started?
    
    //strings are char characters
    
    playerName = @"Jyoubu"; //player's name
    inputText = @""; //the command or text the player has just typed
    onGoingDisplayText = @""; //what the player has typed so far
}

-(IBAction) textSubmitted: (id) sender
{ //run the code for when text has been submitted via the input area by pressing enter.

    

    //sort out strings



    NSString *introNameString;
    
    NSString *onGoingDisplayText2 = [mainTextView stringValue];//make a temp string of the current string value to stop crashes
    
    inputText = [inputTextView stringValue]; //save the input text
    
    inputText = [inputText lowercaseString];
    
    //const char *tempChar = (char *)[inputText UTF8String]; //convert the input text to all lower case
    NSLog(@"player name: %@", playerName);
    
    if(languageSelect == 0){
        introNameString = [NSString stringWithFormat: @"Greetings %@ %@", playerName, introStringEng];
    }
}

Solution

  • There's nothing wrong with accessing NSString from an IBAction.

    The problem is with the memory management. This error message (BAD_EXEC_ACCESS) means you are trying to access a deallocated object.

    Since ARC (Automatic Reference Counting is not available for Tiger, and only properly supported from 10.7 Lion the memory is not managed automatically.

    https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

    https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226

    If the program relies on ARC, it won't work in older systems (crashes due to access to deallocated objects).

    You could write manual retains and releases like in pre-ARC times, but that would mean you would have to manage memory manually everywhere. Possible, but not fun.