SIGTRAP on adding object to mutable array

  • Hi All,

    I am attempting to add an object to an NSMutableArray, and when I do,
    I get a SIGTRAP error. I have tried a bunch of different ways of
    doing this and can't seem to find the problem. I did almost the exact
    same thing in some other code and it worked fine, so I am totally
    mystified.

    Here is the relevant code. Thanks in advance.

    Daniel

    (for a structure like this)
    @interface WordParser : NSObject
    {
    NSMutableArray *syllables;
    NSMutableArray *wordCandidates;
    NSMutableString *currentCandidate;
    NSString *head;
    NSString *tail;
    }

    (implementation to follow)
    - (NSMutableArray *) deriveCandidatesFromSyllables {
    int i, numSyllables, dividingPoint;
    NSString *tempHead, *tempTail;
    NSMutableString *tempCandidate = [[NSMutableString alloc] init];

    numSyllables = [[self syllables] count]; // syllables is an instance
    variable conttaining array of syllables
    dividingPoint = numSyllables;

    for (i = numSyllables; i > 0; i--) {
      tempHead = [self getHeadUpToDividingPoint: dividingPoint]; // THIS
    WORKS
      tempTail = [self getTailAfterDividingPoint: dividingPoint]; // THIS
    WORKS

      // tempCandidate will be the head + space plus the tail
      // ACTUALLY SHOULD ADD IN MORE SOPHISTICATED MANNER
      [tempCandidate appendString: tempHead];
      [tempCandidate appendString: SPACE];
      [tempCandidate appendString: tempTail];

      [self setHead: tempHead];
      [self setTail: tempTail];
      [self setCurrentCandidate: tempCandidate]; // THIS HAS A VALUE
      [self addCandidate: tempCandidate]; // SIGTRAP OCCURS HERE
    }
    [tempCandidate release];
    return wordCandidates;
    }

    - (void) addCandidate: (NSString *) c
    {
    [[self wordCandidates] addObject: c];  // wordCandidates is an
    NSMutableArray ivar
        // SIGTRAP HERE
    // ALSO TRIED [wordCandidates addObject: c];
    // DO I NEED TO COPY OR RETAIN HERE?
    }
  • Where is wordCandidates alloc'd and init'd?

    On Sep 11, 2007, at 5:23 PM, Daniel Child wrote:

    > Hi All,
    >
    > I am attempting to add an object to an NSMutableArray, and when I
    > do, I get a SIGTRAP error. I have tried a bunch of different ways
    > of doing this and can't seem to find the problem. I did almost the
    > exact same thing in some other code and it worked fine, so I am
    > totally mystified.
    >
    > Here is the relevant code. Thanks in advance.
    >
    > Daniel
    >
    > (for a structure like this)
    > @interface WordParser : NSObject
    > {
    > NSMutableArray *syllables;
    > NSMutableArray *wordCandidates;
    > NSMutableString *currentCandidate;
    > NSString *head;
    > NSString *tail;
    > }
    >
    > (implementation to follow)
    > - (NSMutableArray *) deriveCandidatesFromSyllables {
    > int i, numSyllables, dividingPoint;
    > NSString *tempHead, *tempTail;
    > NSMutableString *tempCandidate = [[NSMutableString alloc] init];
    >
    > numSyllables = [[self syllables] count]; // syllables is an
    > instance variable conttaining array of syllables
    > dividingPoint = numSyllables;
    >
    > for (i = numSyllables; i > 0; i--) {
    > tempHead = [self getHeadUpToDividingPoint: dividingPoint]; //
    > THIS WORKS
    > tempTail = [self getTailAfterDividingPoint: dividingPoint]; //
    > THIS WORKS
    >
    > // tempCandidate will be the head + space plus the tail
    > // ACTUALLY SHOULD ADD IN MORE SOPHISTICATED MANNER
    > [tempCandidate appendString: tempHead];
    > [tempCandidate appendString: SPACE];
    > [tempCandidate appendString: tempTail];
    >
    > [self setHead: tempHead];
    > [self setTail: tempTail];
    > [self setCurrentCandidate: tempCandidate]; // THIS HAS A VALUE
    > [self addCandidate: tempCandidate]; // SIGTRAP OCCURS HERE
    > }
    > [tempCandidate release];
    > return wordCandidates;
    > }
    >
    > - (void) addCandidate: (NSString *) c
    > {
    > [[self wordCandidates] addObject: c];  // wordCandidates is an
    > NSMutableArray ivar
    > // SIGTRAP HERE
    > // ALSO TRIED [wordCandidates addObject: c];
    > // DO I NEED TO COPY OR RETAIN HERE?
    > }
  • On 9/11/07, Daniel Child <wchild...> wrote:

    > - (void) addCandidate: (NSString *) c
    > {
    > [[self wordCandidates] addObject: c];  // wordCandidates is an
    > NSMutableArray ivar
    > // SIGTRAP HERE
    > // ALSO TRIED [wordCandidates addObject: c];
    > // DO I NEED TO COPY OR RETAIN HERE?
    > }

    It sounds like wordCandidates in pointing at an object that no longer
    exits. So what code touches wordCandidates? How is that object
    created? etc.

    -Shawn
  • That's what I would have expected, but during debugging,
    wordObjects had an address and was listed as having 0 objects.

    However, your suggesting I look these over helped me uncover the error.
    I had declared the instance variables to be of one type and actually
    allocated memory for another type. Amazingly, XCode did not catch
    this or even
    generate a warning.

    Thanks.

    /************************************************************
    CONSTRUCTORS */
    + (WordParser *) sharedParser
    {
    if (wordParser == nil) {
      wordParser = [[WordParser alloc] init];
    }
    return wordParser;
    }

    /***************************************************** WORD PARSER'S
    INIT */
    - (id) init
    {
    if (self = [super init]) {
      [self setSyllables:      [[NSMutableDictionary alloc] init]]; //
    SOURCE OF ERROR
      [self setWordCandidates: [[NSMutableDictionary alloc] init]]; //
    SOURCE OF ERROR
    }
    return self;
    }

    /************************************************************ SETTERS */
    - (void) setWordCandidates: (NSMutableArray *) wc
    {
    if (wc != wordCandidates) {
      [wordCandidates release];
      wordCandidates = [wc mutableCopy];
    }
    }

    Up to the critical point in the code, I have had no occasion to
    dealloc the shared parser, and
    since the instance variable wordCandidates is not autoreleased, it
    should have stuck around.

    On Sep 11, 2007, at 5:37 PM, Shawn Erickson wrote:

    > On 9/11/07, Daniel Child <wchild...> wrote:
    >
    >> - (void) addCandidate: (NSString *) c
    >> {
    >> [[self wordCandidates] addObject: c];  // wordCandidates
    >> is an
    >> NSMutableArray ivar
    >> // SIGTRAP HERE
    >> // ALSO TRIED [wordCandidates addObject: c];
    >> // DO I NEED TO COPY OR RETAIN HERE?
    >> }
    >
    > It sounds like wordCandidates in pointing at an object that no longer
    > exits. So what code touches wordCandidates? How is that object
    > created? etc.
    >
    > -Shawn
  • On 9/11/07, Daniel Child <wchild...> wrote:
    > That's what I would have expected, but during debugging,
    > wordObjects had an address and was listed as having 0 objects.
    >
    > However, your suggesting I look these over helped me uncover the error.
    > I had declared the instance variables to be of one type and actually
    > allocated memory for another type. Amazingly, XCode did not catch this or
    > even generate a warning.

    -[NSObject init] has a return type of id which is effectively
    type-less (it can point to any object). It is valid to assign a value
    of type id to any object ivar regardless of that ivars type.

    I don't see how you got the failure reported earlier given the code
    you posted here. You should have gotten an exception outlining an
    attempt to send an addObject: message to an instance of
    NSMutableDictionary which doesn't support such a message. It sounds
    like some other issues was corrected around the same time you noticed
    you allocated an object of the wrong type.

    -Shawn
previous month september 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
Go to today