Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

random data loss?

kend0g187kend0g187 Posts: 60Registered Users
I have a custom object, of which I have an array within another custom object, and I have an array of those in my App Delegate. Something like this:

SubObject : NSObject {
some properties...
}

MyObject : NSObject {
some properties...
NSMutableArray *subObjects
}

AppDelegate : NSObject {
NSMutableArray *myObjects
}

In my code I initialize the myObjects array with properties set on both the myObjects and the subObjects, everything working fine. Then, at some future point, during some code that has nothing to do with the myObjects array, the subObjects all of a sudden lose their data. What could cause this?
Post edited by kend0g187 on

Replies

  • kend0g187kend0g187 Posts: 60Registered Users
    Is there something special you have to do when you have two NSMutableArrays of custom objects nested within each other? I'm getting all kinds of data loss/overwriting. Plus, Instruments is telling me I have memory leaks even though I have all the right release calls. Anyone have a hint as to whats happening? Thanks.
  • timle8n1timle8n1 Posts: 414Registered Users
    kend0g187;287990 said:
    Is there something special you have to do when you have two NSMutableArrays of custom objects nested within each other? I'm getting all kinds of data loss/overwriting. Plus, Instruments is telling me I have memory leaks even though I have all the right release calls. Anyone have a hint as to whats happening? Thanks.
    You show no code and claim no leaks though apparent Instruments thinks differently. How is someone supposed to help you?
  • kend0g187kend0g187 Posts: 60Registered Users
    @interface Tile : NSObject {
    BOOL pressed;
    int row;
    int column;
    int section;
    int value;
    }

    @interface Section : NSObject {
    BOOL solved;
    int sectionRow;
    int sectionCol;
    NSMutableArray *tiles;
    }

    for( int r = 1; r <= 9; r++ ) {
    for( int c = 1; c <= 9; c++ ) {
    Section *sec = [[Section alloc] init];
    sec.solved = NO;
    sec.sectionRow = r;
    sec.sectionCol = c;
    sec.tiles = [[[NSMutableArray alloc] initWithCapacity: 9] autorelease];
    for( int t = 1; t <= 9; t++ ) {
    Tile *aTile = [[Tile alloc] init];
    aTile.pressed = NO;
    aTile.value = t;
    aTile.row = (r-1)*3 + (t-1)/3 + 1;
    aTile.column = (c-1)*3 + (t-1)%3 + 1;
    [sec.tiles addObject: aTile];
    [aTile release];
    }
    [sections addObject: sec];
    [sec release];
    }
    }


    Sorry, I wasn't sure if the code would help. But here it is.
  • kend0g187kend0g187 Posts: 60Registered Users
    I was able to fix my problem, by removing the release calls to "sec" and "aTile". Of course, now I have memory leaks. Can anyone shed some light on what's happening? Thanks.
  • timle8n1timle8n1 Posts: 414Registered Users
    kend0g187;288086 said:
    I was able to fix my problem, by removing the release calls to "sec" and "aTile". Of course, now I have memory leaks. Can anyone shed some light on what's happening? Thanks.
    What does the property line or setter look like for tiles?
  • smashersmasher Posts: 3,858Registered Users
    The code you posted looks correct, *assuming* that the "sections" array does not get leaked, that you've written a proper dealloc method for the Section class, and that "tiles" is a retain property. Removing those releases is not correct, as you figured out.

    Whenever you see leaks make sure you tackle the collection classes and parent objects first. If you forget to release one array and it contains 50 strings, you'll see 51 leaks in the leaks instrument. When you fix the array leak they all go away.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • kend0g187kend0g187 Posts: 60Registered Users
    Thanks for the help guys. I think my properties are correct:

    In Section.h I have:

    @property (nonatomic, retain) NSMutableArray *tiles;

    In Section.m I have:

    - (void) dealloc {
    [tiles release];
    [super dealloc];
    }

    Elsewhere in my code, after the sections array is initialized, I have this:

    Section *sec = [[Section alloc] init];
    sec = [sections objectAtIndex: index];
    ...
    [sec release];


    Is that correct, or could that be part of the problem?
  • smashersmasher Posts: 3,858Registered Users
    kend0g187;288295 said:

    Elsewhere in my code, after the sections array is initialized, I have this:

    Section *sec = [[Section alloc] init];
    sec = [sections objectAtIndex: index];
    ...
    [sec release];


    Is that correct, or could that be part of the problem?
    Yoinks! That's not correct. You're creating a new pointer, and alloc+init a new Section for it to point to.

    Then you point the pointer (sec) at a different object, leaking one you just alloc'd.

    Then you release the object that you got from the sections array, even though you never retained it. I am surprised that doesn't crash your program.

    Section *sec =[sections objectAtIndex: index];
    // do stuff with sec
    // no need to release; we didn't retain it.


    Your snippet indicates a confusion between pointers and objects. You can have two pointers to the same object, and you don't need to alloc a new object every time you have a new pointer. That confusion is probably the root of all of your issues.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • kend0g187kend0g187 Posts: 60Registered Users
    I'm seem to be getting it. I'm now down to just two memory leaks, with the program working. But I'm confused about one thing. I have this code:

    - (id) copyWithZone: (NSZone *) zone {
    Section *sectionCopy = [[Section allocWithZone: zone] init];
    sectionCopy.solved = solved;
    sectionCopy.sectionRow = sectionRow;
    sectionCopy.sectionCol = sectionCol;
    sectionCopy.tiles = [[NSMutableArray alloc] init];
    for( Tile *aTile in tiles ) {
    Tile *tileCopy = [aTile copy];
    [sectionCopy.tiles addObject: tileCopy];
    [tileCopy release];
    }
    //[sectionCopy.tiles release];
    return sectionCopy;
    }


    The alloc-ing of the Mutable Array seems to be giving me a leak, but if I uncomment the release, my app crashes, and if I don't alloc the array then I can't add objects to it. What is the solution here?
  • smashersmasher Posts: 3,858Registered Users
    I'd write that line like this:

    sectionCopy.tiles = [NSMutableArray array];


    That creates an autoreleased array, but property will retain it. Now you don't need to call [sectionCopy.tiles release] here.

    If you make this change and your app starts crashing then you are over-releasing "tiles" somewhere else.

    Remember that the leaks tool tells you where the leaked object was created, *not* the line that is wrong, broken, or "causing" the leak.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
Sign In or Register to comment.