3

I was doing a debug on an application I'm creating, but running instruments I found a memory leak that I can't be able to understand.

If I try to modify the code following Instruments suggestion, my application crashes because of

message sent to deallocated instance

Can someone help me?

- (void) objectAtIndex:(int)index {
    SpecialObject *specialObj = [SpecialObject sharedInstance];

    id model = [self.datasource objectAtIndex:index];

    if ([model isKindOfClass:[ClassA class]]) {
        ClassA *objA = (ClassA *)model;
        specialObj.title = objA.title;

    } else if ([model isKindOfClass:[ClassB class]]) {
        ClassB *objB = (ClassB *)model;
        specialObj.title = objB.title;
    }
}

self.datasource is a NSMutableArray while specialObj.title is a NSString

They are defined as @property(nonatomic, retain).

My problem is that Instruments tells me that these 2

ClassA *objA = (ClassA *)model;
ClassB *objB = (ClassB *)model;

are leaks, but if I release objA and objB my application crash.

Thanks for any help!

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Marco Pace
  • 3,820
  • 19
  • 38
  • What type of object is dataSource? Does it definitely return an autoreleased object from that method? – Paul.s May 23 '12 at 14:41
  • Hi Paul and thanks: it is a NSMutableArray (I've edited the first post). – Marco Pace May 23 '12 at 14:45
  • Could you provide `[self.datasource objectAtIndex:index]` implementation? – Lorenzo B May 23 '12 at 14:51
  • It is a method of the NSArray class, it isn't mine. – Marco Pace May 23 '12 at 14:54
  • sorry, I meant `self.dataSource`. I did copy and paste and I forgot to delete the second part. – Lorenzo B May 23 '12 at 14:56
  • Don't worry :), it is defined as @property(nonatomic, retain): @property(nonatomic, retain) NSMutableArray dataSource; – Marco Pace May 23 '12 at 14:58
  • Ok, maybe you can post the code you use to init that array. Furthermore, are you using ARC? – Lorenzo B May 23 '12 at 15:00
  • I initialize the array in the viewDidLoad method with [NSMutableArray array]; . And no, i can't use ARC :-( – Marco Pace May 23 '12 at 15:03
  • Are you running instruments in the simulator or on a device? Instruments will give you false positives in the simulator – slf May 23 '12 at 16:35
  • Your "instruments" are pointing at the wrong lines of code. Your leak is not in that method, unless it's something to do with the implementation of `sharedInstance`. – Hot Licks May 23 '12 at 18:06
  • Thanks, the problem was in another point of the application, I didn't release a variable in my dealloc. If you write an answer I'll accept it. But why XCode give me an error here? – Marco Pace May 24 '12 at 09:03

3 Answers3

0

My guess is that you never release the the data source. Assuming you are not using ARC, in your dealloc method, you should have

[self setDataSource: nil];

or

[instanceVariableThatBacksDataSourceProperty release];

Many people do the former, but Apple recommends the latter so that KVO is not triggered during deallocation.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
0

The leaks and your exception are different things. The exception is almost certainly caused by releasing something you should not have released.

The fact that you have both suggests that your understanding of Objective-C memory management is less than stellar.

You should concentrate first on understanding the exception, and the first step toward doing that is to look at the exception traceback.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Dear Hot Licks, the problem is not that I have leaks and exception; my problem is that if I release object following XCode my app crashes and if I don't do that, it tell me is a leak. So if you understanding is better than me, and I'm sure it is because I'm a novice, what the solution of my question? And I'm able to read exception traceback, it comes back to the point I post in my question. – Marco Pace May 23 '12 at 16:02
0

After some search I've found the problem: XCode usually tells where the leaks are, but some times it give you a probable position for it.

This means that the memory leak is somewhere in the program flow the goes to the point XCode indicates, not necessarily exactly where it says.

Hope this can help someone else in the future!

Marco Pace
  • 3,820
  • 19
  • 38