Accessing data within a class structure in Objective C

Discussion in 'Mac Programming' started by dbrayford, Aug 19, 2010.

  1. dbrayford macrumors member

    Feb 22, 2010
    I am new to Objective C and am looking at some code that releases memory back to the system using [MyData release], but get a crash as some of the memory has already been returned to the system.

    The object MyData has the following structure:
    MyData* (NSCFArray)MyFiles* FileParser* DataCollection*

    The object DataCollection* causes the crash because of a double free. I want to be able to check that the DataCollection* object hasn't had it's memory released back to the system.

    I just don't know how to do it in objective C.

    I am looking to do something like this. I apologies for the psuedocode, but am still learning the syntax for objective C
    // check if the sub-object has been released
    if( ! MyData* MyFiles*  FileParser* DataCollection* )
        // allocate some memory to the sub-object. 
        // Just a quick fix while I track down where the problem is
        MyData* MyFiles*  FileParser* DataCollection* = new MyData* MyFiles*   FileParser* DataCollection*;
    // release the main objects memory back to the system
    [MyData release]
  2. chown33 macrumors 604

    Aug 9, 2009
    What real computer languages do you know?

    Your data structure pseudo-code isn't making any sense. In particular, the meaning of * in the following:
    MyData* (NSCFArray)MyFiles* FileParser* DataCollection*
    Does this mean you have a MyData reference pointing to a MyFiles reference pointing to a FileParser reference pointing to a DataCollection reference? That's the first interpretation I get, but that's a helluva freakin' complicated series of pointer references, and it doesn't use the correct C or Obj-C notation.

    I think you should post the actual code, including the types of the actual variables. Even if it doesn't compile, post it along with the text of the error messages you get. I'm guessing you do have something that compiles, though, or you wouldn't be asking about a runtime error. So post the actual code that you're running.

    Or maybe you can run your program under Instruments or with NSZombieEnabled and find your double-release that way. You can google both those terms to find more info.
  3. jared_kipe macrumors 68030


    Dec 8, 2003
    Note the "MyData* ... = new MyData*...;"

    I'm gonna make the fabulous leap and say this is supposed to be some kind of bastard java . syntax

  4. Peter Maurer macrumors member

    Oct 9, 2008
    Regardless of the star-separated object structure I don't understand either, I'll say this: It's almost never a good idea to decide whether to release an object based on its current -retainCount. If the object has already been autoreleased, the retain count will still be above zero, but you can't release it unless you want your app to crash. Also, consider a scenario where another object that's currently working with the data object has retained it temporarily when you check. Again, you'll over-release.

    In short: don't guess based on the current state, just know what you're responsible for. The whole idea behind the retain/release construct is to free you from having to decide when to deallocate. All you have to do is say "I'm done with this" via -release, and the object itself will know when to deallocate.

    So the easiest way to solve this is finding the part of your code that releases the object prematurely ("some of the memory has already been returned to the system"), and fix that. If for some reason you can't do that, you'll probably have to add a flag property to the object in question's "parent object" (assuming the structure you're describing is a hierarchical one) that can store that object's "already released" state, then check that flag in the parent object's -dealloc.
  5. gnasher729 macrumors P6


    Nov 25, 2005
    That doesn't make any sense. Please post the @interface part of your class insted. And it seems that you are trying to mix C++ with Objective-C, but it is hard to say without actual code (seeing "new" in Objective-C code makes me nervous).

    In general, the rule is that if you want ownership of an object, you retain it, and you release it. Once. Others may have ownership of the same object, they do the same thing. The object disappears when everyone has released it. "autorelease" means "release it some time later automatically".

    [[alloc] init] creates an object that has been retained once for you, so you own it and at some point must release it. Any method with "create" or "copy" creates an object that has been retained once for you, same thing. Anything else returns objects that you don't own (yet); if you want to own it, you must retain it otherwise it can go away at any time.

    All objects that an object owns must be released in [dealloc].
  6. dbrayford thread starter macrumors member

    Feb 22, 2010
    Sorry I am not an objective C programmer (C programmer trying to understand the weird syntax.) and am trying to fix another persons code.

    I fixed the problem it was a call to [super dealloc].

    I have now hit another problem I have an invalid object (debugger displays the object value to be invalid) that calls release.

    How do I determine if an object is invalid?

    Is invalid in objective C null?
  7. jared_kipe macrumors 68030


    Dec 8, 2003
    Where was the [super dealloc]? It should be the last line of -dealloc
    If it wasn't then it was probably a bug. If it was, then you are just shifting the bug to somewhere else.

    Invalid usually comes up when the memory has been freed up or used by something else and you still have a pointer pointing there.

    The values inside that "object" is invalid in the debugger because the objects structure isn't there anymore.
  8. chown33 macrumors 604

    Aug 9, 2009

Share This Page