some crash i can not explain.

  • Greetings!
    I'm learning cocoa! :)
    the explaination of my problem will be as clear as i can understand
    it so please bear with me :)

    i'm trying to fill a NSMutableArray with some custom objects
    My custom calss is defined like this :

    ----------------------------------
    @interface ADTreeViewItem : NSObject {
    // the index of the item in the list.
    int index;
    NSString *displayString;
    int refIndex;
    }
    -(oneway void) release;
    -(void)setIndex:        (int)value;
    -(void)setDisplayString: (NSString *)value;
    -(void)setRefIndex:      (int)value;

    -(int)getIndex;
    -(NSString *)getDisplayString;
    -(int)getRefIndex;
    @end
    ----------------------------------
    @implementation ADTreeViewItem
    - (oneway void) release{
    [displayString release]; // i have a retain on it.
    }
    -(void)setIndex:(int)value{
    if (value <= 0) value = 0; // no negative index.
    index = value;
    }
    -(void)setDisplayString:(NSString *)value{
    // retain the string so it does not go away...
    displayString = [value retain];
    }
    -(void)setRefIndex:(int)value{
    refIndex = value;
    }

    -(int)getIndex{
    return index;
    }
    -(NSString *)getDisplayString{
    return displayString;
    }
    -(int)getRefIndex{
    return refIndex;
    }
    @end
    ---------------------------------------
    ok now, when i try to add an instance of this class to a array like
    this.
    the compiler does not complain, but the runtime send's me to the debug
    window, without any explaination, i just get a window with ASM code

    another custom class holds 3 Mutable Array.
    but i've cut them out for this mail... and there all init and
    released the same.
    @implementation ADTreeViewData
    //init
    -(id)init{
    if ((self = [super init])) {
      arrayOfNames = [[NSMutableArray array] init];
    }
    return self;
    }
    // clean up
    - (oneway void)release{
    [arrayOfNames release];
    [super release];
    }

    -(int)addName:(NSString *)name{
    ADTreeViewItem *item = [[ADTreeViewItem alloc]init];
    [item setDisplayString:name];
    [arrayofNames addObject:item];
    return [arrayOfNames count];
    }

    so , when i add the created item to the arrayOfNames i get  no error
    but everything stops, I've checked the variables in the debug view
    and the array is not null...in the sence that is has been created and
    is not released at the point where i am trying to insert an object
    into it....

    can anyone help out or point out what i'm doing wrong ?

    thank you in advance.
    Sandro.
  • On Oct 23, 2006, at 12:15 PM, Sandro Noel wrote:

    > - (oneway void) release{
    > [displayString release]; // i have a retain on it.
    > }

    Don't ever do this. Not only does the above not invoke the superclass
    implementation (it should) but you should override -dealloc instead.
    In general there are few reasons to override -release, -retain, or -
    autorelease in subclasses.

    > -(void)setDisplayString:(NSString *)value{
    > // retain the string so it does not go away...
    > displayString = [value retain];
    > }

    This will cause a memory leak if displayString was set to something
    else. Try this instead:

    if (value != displayString)
    {
    [displayString release];
    displayString = [value retain];
    }

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • First off, thank you Nick.

    I corrected every location where i had made the mistake,
    but i still get the runtime error, in this block of code.

    -(int)addName:(NSString *)name{
    ADTreeViewItem *item = [[ADTreeViewItem alloc]init];
    [item setDisplayString:name];
    [arrayofNames addObject:item];
    return [arrayOfNames count];
    }

    I was thinking that the "item" variable was not in a good place, and
    was released after the function ended,and that might cause problems
    but then i red up on Array's and they retain objects, so it should
    not matter where the object is allocated from.
    [arrayofNames addObject:item];
    this somehow crashes the app without an error message in the NSLog.

    Any help on this would be greatly appreciated!
    and thank again! learning cocoa this far is fun :)

    Sandro Noel.

    On 23-Oct-06, at 2:29 PM, Nick Zitzmann wrote:

    >
    > On Oct 23, 2006, at 12:15 PM, Sandro Noel wrote:
    >
    >> - (oneway void) release{
    >> [displayString release]; // i have a retain on it.
    >> }
    >
    > Don't ever do this. Not only does the above not invoke the
    > superclass implementation (it should) but you should override -
    > dealloc instead. In general there are few reasons to override -
    > release, -retain, or -autorelease in subclasses.
    >
    >> -(void)setDisplayString:(NSString *)value{
    >> // retain the string so it does not go away...
    >> displayString = [value retain];
    >> }
    >
    > This will cause a memory leak if displayString was set to something
    > else. Try this instead:
    >
    > if (value != displayString)
    > {
    > [displayString release];
    > displayString = [value retain];
    > }
    >
    > Nick Zitzmann
    > <http://www.chronosnet.com/>
    >
    >
    >
    >
    >
  • On Oct 23, 2006, at 3:10 PM, Sandro Noel wrote:

    > First off, thank you Nick.
    >
    > I corrected every location where i had made the mistake,
    > but i still get the runtime error, in this block of code.
    >
    > -(int)addName:(NSString *)name{
    > ADTreeViewItem *item = [[ADTreeViewItem alloc]init];
    > [item setDisplayString:name];
    > [arrayofNames addObject:item];
    > return [arrayOfNames count];
    > }
    >
    > I was thinking that the "item" variable was not in a good place,
    > and was released after the function ended,and that might cause
    > problems but then i red up on Array's and they retain objects, so
    > it should not matter where the object is allocated from.
    > [arrayofNames addObject:item];
    > this somehow crashes the app without an error message in the NSLog.

    I think you might also be having trouble with the line

    arrayOfNames = [[NSMutableArray array] init];

    The way you've done it, the array won't be retained because -array is
    a convenience method and will return an autoreleased object. You
    could use

    arrayOfNames = [[NSMutableArray array] retain];

    or

    arrayOfNames = [[NSMutableArray alloc] init];

    instead. For the latter, you won't need a -retain message because
    arrayOfNames is an instance variable in your class.

    HTH,

    Glenn Zelniker
  • On 23 Oct 06, at 12:10, Sandro Noel wrote:
    > First off, thank you Nick.
    >
    > I corrected every location where i had made the mistake,
    > but i still get the runtime error, in this block of code.
    >
    > -(int)addName:(NSString *)name{
    > ADTreeViewItem *item = [[ADTreeViewItem alloc]init];
    > [item setDisplayString:name];
    > [arrayofNames addObject:item];
    > return [arrayOfNames count];
    > }
    >
    > I was thinking that the "item" variable was not in a good place,
    > and was released after the function ended,and that might cause
    > problems but then i red up on Array's and they retain objects, so
    > it should not matter where the object is allocated from.
    > [arrayofNames addObject:item];
    > this somehow crashes the app without an error message in the NSLog.
    >
    > Any help on this would be greatly appreciated!
    > and thank again! learning cocoa this far is fun :)

    Read the Cocoa memory guidelines. I think there are some points
    you're still a little unclear on.

    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
    MemoryMgmt.html
  • Glenn, Thank you for the insight and the sample code!
    alloc resolved my problem.

    but i still do not understand "why" the array was released before i
    had a chance to use it... i tought that since it was an instance
    variable it was "owned" by the instance
    and that a "retain" was not needed whatever i created the object with...
    oh well, more reading to do :P

    Andrew, thank you for the reference, I had read it, but i must have
    missed something.
    will do my homework again :)

    Thank you much for the assistance! it is appreciated!

    Sandro Noel.

    On 23-Oct-06, at 3:24 PM, Glenn Zelniker wrote:

    >
    > On Oct 23, 2006, at 3:10 PM, Sandro Noel wrote:
    >
    >> First off, thank you Nick.
    >>
    >> I corrected every location where i had made the mistake,
    >> but i still get the runtime error, in this block of code.
    >>
    >> -(int)addName:(NSString *)name{
    >> ADTreeViewItem *item = [[ADTreeViewItem alloc]init];
    >> [item setDisplayString:name];
    >> [arrayofNames addObject:item];
    >> return [arrayOfNames count];
    >> }
    >>
    >> I was thinking that the "item" variable was not in a good place,
    >> and was released after the function ended,and that might cause
    >> problems but then i red up on Array's and they retain objects, so
    >> it should not matter where the object is allocated from.
    >> [arrayofNames addObject:item];
    >> this somehow crashes the app without an error message in the NSLog.
    >
    > I think you might also be having trouble with the line
    >
    > arrayOfNames = [[NSMutableArray array] init];
    >
    > The way you've done it, the array won't be retained because -array
    > is a convenience method and will return an autoreleased object. You
    > could use
    >
    > arrayOfNames = [[NSMutableArray array] retain];
    >
    > or
    >
    > arrayOfNames = [[NSMutableArray alloc] init];
    >
    > instead. For the latter, you won't need a -retain message because
    > arrayOfNames is an instance variable in your class.
    >
    > HTH,
    >
    > Glenn Zelniker
    >
    >
    >
  • On Oct 23, 2006, at 3:43 PM, Sandro Noel wrote:

    > but i still do not understand "why" the array was released before i
    > had a chance to use it... i tought that since it was an instance
    > variable it was "owned" by the instance
    > and that a "retain" was not needed whatever i created the object
    > with...

    No offense intended, but that's completely wrong. Do yourself a favor
    and read this:

    <file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/
    Conceptual/MemoryMgmt/index.html>

    The same doc, on the web:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
    index.html
    >

    sherm--

    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
  • Sherm, no offense taken!
    I'm in a learning curve , so every constructive comment is welcome.

    thank you!
    Sandro Noel.

    On 23-Oct-06, at 3:57 PM, Sherm Pendley wrote:

    > On Oct 23, 2006, at 3:43 PM, Sandro Noel wrote:
    >
    >> but i still do not understand "why" the array was released before
    >> i had a chance to use it... i tought that since it was an instance
    >> variable it was "owned" by the instance
    >> and that a "retain" was not needed whatever i created the object
    >> with...
    >
    > No offense intended, but that's completely wrong. Do yourself a
    > favor and read this:
    >
    > <file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/
    > Conceptual/MemoryMgmt/index.html>
    >
    > The same doc, on the web:
    >
    > <http://developer.apple.com/documentation/Cocoa/Conceptual/
    > MemoryMgmt/index.html>
    >
    > sherm--
    >
    > Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    > Cocoa programming in Perl: http://camelbones.sourceforge.net
    >
    >
    >
previous month october 2006 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