0

In my project the data read from database is assigned to an allocated object. After the object is added to an NSMutableArray. And then the object is released. Is there any problem with doing like this? My source code:

     while (sqlite3_step(statement) == SQLITE_ROW) 
            {   
                TopicEntry *temp = [[TopicEntry alloc] init];
                ruleName = (char *)sqlite3_column_text(statement, 2);

                [temp setTopicName:(ruleName) ?[NSString stringWithUTF8String:ruleName] :@""];
                [temp setParentID:(int)sqlite3_column_int(statement, 2)];
                [rules addObject:temp];
                [temp release];

            }

3 Answers 3

1

Your code is OK.

The only thing you need to think about is that when you own an object, you need to release it. Calling the init method makes you own it, so according to policy you have to release it, regardless of what you do with it. You can read all about the policy here.

Furthermore, the NSMutableArray will retain objects passed to it via addObject. But this is not how you should think about it, and can be considered an implementation detail. All that matters is the ownership policy.

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

1 Comment

i have a doubt, please help me. if i wrote like: TopicEntry *t = [TopicEntry alloc] init]; t = [topic objectAtIndex:0]; //did some stuff [t release]; is this code will release the actual object in the NSMutableArray? Please help, i'm a fresher to this field
0

No, your code looks fine as far as the memory allocation is concerned.

Comments

0

Id say that was exactly the right way to do it. rules will retain the temp object when it's added and release it when it is released (Or the object is removed).

Comments

Your Answer

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