2

I'm seeing a weird behavior and I would need some help with it.

In structure.h I have:

typedef struct {
    NSString *summary;
    NSArray *legs;
    NSString *copyrights;
    struct polylineSruct overview_polyline;
    struct directionBounds bounds;    
} route;

typedef struct {
    NSArray *routes;
    NSString *status;
} directions;

In structure.m I have:

(directions) a_Function_that_builds_the_struct
{
    directions direct;

    direct.status = @"OK";

    NSMutableArray *routes = [NSMutableArray array];
    for(xxx)
    {
        route routeL;
        routeL.summary = @"1";
        routeL.copyrights = @"2";

        NSValue *boxedroute = [NSValue valueWithBytes:&routeL objCType:@encode(route)];
        [routes addObject:boxedroute];
    }

    direct.routes = routes;

    return direct;
}

in list_itemsViewController.h I have:

implementation XXX:YYY{
    directions directionsLoc;
}
@property (assign) directions directionsLoc;

in list_itemsViewController.h I have:

@synthesize directionsLoc;
....
- (void)viewDidLoad
{
    ....
    self.directionsLoc = a_Function_that_builds_the_struct;
    ....
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [directionsLoc.routes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    cell.textLabel.text = directionsLoc.status
    return cell;
}

When I start the application I get the list with all the correct rows, if I scroll a little bit for the tableView:cellForRowAtIndexPath: the property directionsLoc is deallocated.

Does anybody have any idea why I get this problem? Is it because I use typedef and the retention is not kept? If I return in the a_Function_that_builds_the_struct and NSArray of directions when the scrolling happens and tableView:cellForRowAtIndexPath: is executed the array had one element as expected but the status and routes elements of the object are zombies.

Any thought on why this happens?

Thank you.

3
  • wrap your structs by an NSObject and make sure they are autoreleased Commented Jul 6, 2011 at 20:16
  • 3
    For such structs, you would be far better off simply declaring a class with the struct members as ivars and/or @properties. There is no significant overhead in doing so, it will be significantly simpler to maintain over time and it is much more future proof. Commented Jul 6, 2011 at 20:29
  • I have about 7-8 structs that are linked to get a big and easy to access struct, in the question I gave a quick one with just two structs. Is it worth creating 7-9 classes with 2-5 ivars? Ultimately I'm trying to put a JSON response, received from google code.google.com/apis/maps/documentation/directions/#JSON, from a NSDictionary into something easy to use while using lists and easy to get the number or routes/legs/steps. Commented Jul 6, 2011 at 20:46

1 Answer 1

10

Structs follow exactly the same rules in Objective-C as C, as it's a strict superset. So a direct assignment is a shallow copy, and the concept of deallocating just directions directionsLoc; doesn't really make sense.

However, you've created at least one of the things that is stored within the directions struct — the routes array — as an autoreleased object. Therefore it will be released when next the current autorelease pool is drained, which won't happen at least until the stack unwinds if you're not doing anything in that area yourself.

So the problem isn't the struct at all, it's the normal memory management rules, possibly combined with the fact that storing values to a struct looks like Objective-C 2.0 syntax for setting and getting properties but doesn't involve any sort of method call, so can't in itself retain or copy.

Sign up to request clarification or add additional context in comments.

6 Comments

This is why I recommend against storing objects in structs — it's just a memory management mess with little to no benefit. It's actually forbidden except with an "unsafe" type under the upcoming ARC compiler mode.
OK. I understand that my approach is not the best, what's a better approach than structs?
@genie: Turn your structs into real Objective-C objects. Make a "Route" object and a "Directions" object, which have properties for the various members of the corresponding structs.
@BJ Homer: I have about 7-9 structs in an hierarchy, is the option to create classes for each struct the best approach in this case?
@genie: Yes. Classes are functionally equivalent to structs in most respects, except that they must be allocated on the heap and they will take care not to make your program blow up if you store objects in them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.