Resolved re-initializing an object is causing a crash

Discussion in 'iOS Programming' started by multinode, Mar 9, 2011.

  1. multinode, Mar 9, 2011
    Last edited: Mar 10, 2011

    multinode macrumors regular

    Feb 4, 2011
    I have the following code:
    int main(int argc, char *argv[]) {
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
            FILE *ftest;
    	int size, i;
    	char *string;
    	NSString *myNSString = [NSString alloc];
    	ftest = fopen("/form.txt", "r");
    	stat(name, &stbuf);
    	myBuffer = calloc(1, size = stbuf.st_size);
    	string = myBuffer;
    	fread(myBuffer, 1, size, ftest);
    	for(i=0; i<size; i++){
    		if(myBuffer[i]==0x09) {
     --->>		        myNSString = [myNSString initWithCString:string encoding:1];
    			string = &myBuffer[i+1];
    The problem I have is that reinitializing myNSString (at the line with --->>) throws an exception "selector not recognized" at the NSObject level. Is this a problem with myNSString being auto released? Or what else is my problem please?
  2. yaniv92648 macrumors member

    Oct 26, 2009
    Objective-c beginner tutorial

    Why do u right in C and not Objective-c?

    why r u writing code in the main function?
    no code should be written there except for the existing code in the beginning.

    why r u allocing an object without initializing it?
    NSString *myNSString = [NSString alloc];

    i think u should read a beginner tutorial on objective-c.
  3. multinode thread starter macrumors regular

    Feb 4, 2011
    I wrote code in main() to test out some concepts first ... this is just temporary code.

    I don't initialize immediately because I initialize later in the code.

    The C code is because I need to prepare the data that comes in from the fread() before I do the Obj C operations ... that's more easily done in C. What's shown here is just a snippet of my app.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Don't do that.

    First, most objects can't be reinitialized. init methods are almost never designed to accomodate that.

    Second, NSString is immutable. It's objects are not designed to be mutated after init.
  5. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England

Share This Page