nsoutlineview crashes

  • Hi,

    i am implementing a datasource for an nsoutlineview to represent the
    contents of a simple tree-structure defined by the following class
    (simplified):

    @interface TBCatalogNode : NSObject {
        NSMutableArray* children;
    }
    @property (readonly, retain) NSMutableArray* children;

    (the array children is created by the constructor is definately never nil)

    my data source looks like this:

    -(CatalogRepresentation*) initWithNode:(TBCatalogNode*)rootNode {
        if (self = [super init]) {
            root = [rootNode retain];
        }
        return self;
    }

    - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index
    ofItem:(id)item {
        TBCatalogNode* objItem = (item == nil) ? root : item;
        return [[[objItem children] objectAtIndex:index] retain];
    }

    - (NSInteger)outlineView:(NSOutlineView *)outlineView
    numberOfChildrenOfItem:(id)item {
        return (item == nil) ? [[root children] count] : [[item children]
    count];
    }

    - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
        return ([self outlineView:outlineView numberOfChildrenOfItem:item] > 0);
    }

    - (id)outlineView:(NSOutlineView *)outlineView
    objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
        TBCatalogNode* objItem = (item == nil) ? root : item;
        return [[[objItem preset] info] objectForKey:@"ID"];  // this line gets
    a string from a nsdictionary
    }

    This code works perfect with the garbage collection activated. when i
    disable the gc it is working just at the begining, it shows the first level
    under the root. when i click to expand an item it crashes -> debugger is
    telling me at numberOfChildrenOfItem: - function.
    I dont know what in the background is going on as I am pretty new to OSX. I
    guess it is about some memory management, threading ?! I tried to retain
    nearly everything, and I am running out of ideas.

    The best solution would be to use the garbage collection of course, but I
    need to load some bundles which are not compiled for gc. Is there a way to
    load these bundles anyway? I tried to disable gc for the pointer of the
    bundle, but that failed.

    Thanks in advance
  • On Feb 22, 2008, at 6:51 AM, Julius Eckert wrote:

    > Hi,
    >
    > i am implementing a datasource for an nsoutlineview to represent the
    > contents of a simple tree-structure defined by the following class
    > (simplified):
    >
    > @interface TBCatalogNode : NSObject {
    > NSMutableArray* children;
    > }
    > @property (readonly, retain) NSMutableArray* children;
    >
    > (the array children is created by the constructor is definately
    > never nil)
    >
    > my data source looks like this:
    >
    > -(CatalogRepresentation*) initWithNode:(TBCatalogNode*)rootNode {
    > if (self = [super init]) {
    > root = [rootNode retain];
    > }
    > return self;
    > }
    >
    > - (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index
    > ofItem:(id)item {
    > TBCatalogNode* objItem = (item == nil) ? root : item;
    > return [[[objItem children] objectAtIndex:index] retain];
    > }

    You are responsible for maintaining the life of the objects, not the
    outline view. it does not retain/release the objects. It is incorrect
    to have the last retain in there.

    >
    >
    > - (NSInteger)outlineView:(NSOutlineView *)outlineView
    > numberOfChildrenOfItem:(id)item {
    > return (item == nil) ? [[root children] count] : [[item children]
    > count];
    > }
    >
    > - (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:
    > (id)item {
    > return ([self outlineView:outlineView
    > numberOfChildrenOfItem:item] > 0);
    > }
    >
    > - (id)outlineView:(NSOutlineView *)outlineView
    > objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:
    > (id)item {
    > TBCatalogNode* objItem = (item == nil) ? root : item;
    > return [[[objItem preset] info] objectForKey:@"ID"];  // this
    > line gets
    > a string from a nsdictionary
    > }
    >
    > This code works perfect with the garbage collection activated. when i
    > disable the gc it is working just at the begining, it shows the
    > first level
    > under the root. when i click to expand an item it crashes ->
    > debugger is
    > telling me at numberOfChildrenOfItem: - function.
    > I dont know what in the background is going on as I am pretty new to
    > OSX. I
    > guess it is about some memory management, threading ?! I tried to
    > retain
    > nearly everything, and I am running out of ideas.

    Your model, the TBCatalogNode, needs to retain all the children (which
    it probably does). You are probably doing something wrong when
    inserting children into your model.

    corbin
previous month february 2008 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    
Go to today