Trying to delete the first char of a string?

  • I'm testing catching key events and deleting the first char of a mutable string (here called mstr). The code below works fine and produces output

    [Session started at 2007-10-29 16:19:15 +0100.]
    2007-10-29 16:19:15.700 Task1[1905] Alabama
    2007-10-29 16:19:15.700 Task1[1905] labama
    2007-10-29 16:19:15.704 Task1[1905] Accepting
    2007-10-29 16:19:15.704 Task1[1905] Accepting
    2007-10-29 16:19:15.704 Task1[1905] Accepting
    2007-10-29 16:19:15.704 Task1[1905] Accepting
    2007-10-29 16:19:15.704 Task1[1905] Becoming

    Now if I press the d key on the keyboard I get

    2007-10-29 16:19:18.252 Task1[1905] The input is d

    Here is the header file

    /* MyView.h */

    #import <Cocoa/Cocoa.h>

    @interface MyView : NSView
    {
        NSMutableString *mstr;
    }
    @end

    Here is the implementation file

    * MyView.m */
    #import "MyView.h"

    @implementation MyView

    - (id)initWithFrame:(NSRect)frameRect
    {
        if ((self = [super initWithFrame:frameRect]) != nil) {
            // Add initialization code here
        }
        return self;
    }
    -(void) awakeFromNib
    {
        mstr=[NSMutableString stringWithString:@"Alabama"];
        NSLog(@"%@",mstr);
        // Delete first char
        [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
        NSLog(@"%@",mstr);
    }

    - (void)drawRect:(NSRect)rect
    {
    }

    - (BOOL)acceptsFirstResponder
    {
        NSLog(@"Accepting");
        return YES;
    }

    - (BOOL)resignFirstResponder
    {
        NSLog(@"Resigning");
        [self setNeedsDisplay: YES];
        return YES;
    }

    - (BOOL)becomeFirstResponder{
        NSLog(@"Becoming");
        [self setNeedsDisplay: YES];
        return YES;
    }

    - (void)keyDown:(NSEvent *)event
    {
        [self interpretKeyEvents:[NSArray arrayWithObject:event]];
    }

    - (void)insertText:(NSString *)input
    {
        NSLog(@"The input is %@", input);
        // if I move it here it does not work any more
    }
    @end

    --------------------------------
    So far so good. However if I now move the three lines
    // Delete first char
    [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    NSLog(@"%@",mstr);

    from awakeFromNib method to insertText method so it looks like this
    - (void)insertText:(NSString *)input

    {

        NSLog(@"The input is %@", input);

      // Delete first char
        [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
        NSLog(@"%@",mstr);

    }

    the I get the following output

    [Session started at 2007-10-29 16:17:45 +0100.]
    2007-10-29 16:17:45.528 Task1[1880] Alabama
    2007-10-29 16:17:45.531 Task1[1880] Accepting
    2007-10-29 16:17:45.531 Task1[1880] Accepting
    2007-10-29 16:17:45.531 Task1[1880] Accepting
    2007-10-29 16:17:45.531 Task1[1880] Accepting
    2007-10-29 16:17:45.532 Task1[1880] Becoming
    2007-10-29 16:17:54.421 Task1[1880] The input is d

    Task1 has exited with status -1.

    and I'm thrown out from the program. So it's impossible to delete the first char of the string mstr.

    Can someone explain why this happens?

    Thanks Bob

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
  • On 29 Oct 2007, at 15:25, Bob Ueland wrote:

    > I'm testing catching key events and deleting the first char of a
    > mutable string (here called mstr)

    > @interface MyView : NSView
    > {
    > NSMutableString *mstr;
    > }
    > @end

    ...

    > -(void) awakeFromNib
    > {
    > mstr=[NSMutableString stringWithString:@"Alabama"];
    > }

    ...

    > - (void)insertText:(NSString *)input
    >
    > {
    >
    > NSLog(@"The input is %@", input);
    >
    > // Delete first char
    > [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    > NSLog(@"%@",mstr);
    >
    > }

    mstr is being autoreleased.  Retain it.
  • On 29 Oct 2007, at 10:25 AM, Bob Ueland wrote:

    > -(void) awakeFromNib
    > {
    > mstr=[NSMutableString stringWithString:@"Alabama"];
    > NSLog(@"%@",mstr);
    > // Delete first char
    > [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    > NSLog(@"%@",mstr);
    > }

    Review Cocoa memory management. You initialize mstr with an
    autoreleased instance of NSMutableString. mstr is good within
    awakeFromNib, but once you get back to the run loop (as when you come
    around to insertText:), it points to something that isn't an object
    any more, and you crash.

    — F
  • On 29 Oct 2007, at 15:25, Bob Ueland wrote:

    > -(void) awakeFromNib
    > {
    > mstr=[NSMutableString stringWithString:@"Alabama"];

    [snip]

    > - (void)insertText:(NSString *)input
    >
    > {
    >
    > NSLog(@"The input is %@", input);
    >
    > // Delete first char
    > [mstr deleteCharactersInRange: NSMakeRange (0, 1)];
    > NSLog(@"%@",mstr);
    >
    > }

    Is garbage collection turned on?  If not, the problem here is most
    likely that your "mstr" variable is pointing at a string that has
    already been released (you should do [[NSMutableString alloc]
    initWithString:@"Alabama"] or -retain the result of the -
    stringWithString: call).

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • Thanks Alastair, Jonathan and Fritz. Now it works!

    __________________________________________________
    Do You Yahoo!?
    Tired of spam?  Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today