Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-12-2011, 09:03 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
liligago is on a distinguished road
Exclamation Memory leak in Nested NSDictionary copy sqlite3

I have developed an application which used sqlit3 database. I have made a wrapper class, in this wrapper class I have an NSMutableDictionary and NSMutableArray.
Each time a query is run i used removeAllObjects from the dictonary and the array in the wrapper class (I don't release it). I then add the results from the query to the array and dictionary. The dictionary contains another subdictionaries.
I have 2 sub tableViewController, in these classs ,I get data from the database using my wrapper class and copy it to my tableviewcontroller variable,
but when i change the tableViewController, it is cause the leak in memory,

in the
**brandsRecords = [[FairPriceDB getBrandIDs_NSDictionary]copy];**
i marked as ** and bold the memory leak happends


I appreciate if somebody can guide me to improve these classes and solve the memory leak...
Thank you
CustomeViewController.h
Code:
  @interface BrandViewController : UIViewController
<UITableViewDataSource , UITableViewDelegate>
{
    FairPriceDatabaseView *FairPriceDB;
    NSArray *brandsIDs;
    NSMutableDictionary *brandsRecords;
    UITableView *tableView;  
}
CustomeViewController.m

Code:
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadBrandsIDs];
    [tableView reloadData];
}
- (void)dealloc {
    [super dealloc];
    if (brandsRecords) [brandsRecords release];
    if (brandsIDs) [brandsIDs release];
    if (FairPriceDB) [FairPriceDB release];
}
-(NSArray *) loadBrandsIDs
{
    if (!FairPriceDB) 
        [self loadBrandsDB];
    if(brandsIDs)
        [brandsIDs release];
    brandsIDs = [[FairPriceDB getBrandIDs]copy];
    if (brandsRecords) 
        [brandsRecords release];
    **brandsRecords = [[FairPriceDB getBrandIDs_NSDictionary]copy];**
    [FairPriceDB release];
    FairPriceDB = nil;
    return brandsIDs;
}
- (FairPriceDatabaseView *) loadBrandsDB {
    if (!FairPriceDB) 
        FairPriceDB = [[FairPriceDatabaseView alloc] initWithFairPriceDatabaseViewFilename:@"b.db"];
    return FairPriceDB;
}
FairPriceDataBaseViewController.h (wrapper class)
Code:
@interface FairPriceDatabaseView {
    NSMutableArray * idList;
    NSMutableDictionary * recordList;
}
FairPriceDataBaseViewController.m (wrapper class)
Code:
- (NSArray *) getBrandIDs {
            NSDictionary * row;
            [idList removeAllObjects];  // reset the array
            for (row in [self getQuery:@"SELECT productID,brandName FROM product GROUP BY brandName;"]) 
                [idList addObject:[row objectForKey:@"productID"]];
            return idList;
        }

        -(NSDictionary *) getBrandIDs_NSDictionary{
            [recordList removeAllObjects];
            [idList removeAllObjects];
            [self getBrandIDs];

            NSNumber * rowid;
            for(rowid in [self idList])
                [recordList setObject:[self getProductRow:rowid]  forKey:rowid];

            return recordList; 
        }
    - (NSDictionary *) getProductRow: (NSNumber *) rowid {
        self.tableName = @"select * from product where productID = ?";
        return [self getRow:rowid];
    }
liligago is offline   Reply With Quote
Old 12-12-2011, 09:13 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
- (void)dealloc {
    [super dealloc];
    if (brandsRecords) [brandsRecords release];
    if (brandsIDs) [brandsIDs release];
    if (FairPriceDB) [FairPriceDB release];
}
Oh dear lord, learn memory management.

[super dealloc] should always - ALWAYS - be the last thing you do in this method.

And the mere existence of an object does NOT mean that it should be released, so "if (anything) release" is a ridiculous sequence to go through.

Use properties. Start with the link in my signature if you don't know how.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 12-12-2011, 09:23 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 3
liligago is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Code:
- (void)dealloc {
    [super dealloc];
    if (brandsRecords) [brandsRecords release];
    if (brandsIDs) [brandsIDs release];
    if (FairPriceDB) [FairPriceDB release];
}
Oh dear lord, learn memory management.

[super dealloc] should always - ALWAYS - be the last thing you do in this method.

And the mere existence of an object does NOT mean that it should be released, so "if (anything) release" is a ridiculous sequence to go through.

Use properties. Start with the link in my signature if you don't know how.
this is my first application, and i have just finished Memory management around 1 hour ago. i admitted it, since after i read the memory management i realized this problem. This is my first program, do you think the other issues are correct or not? i have learnt iphone and objetive-c just through web... i appreciate to guide me if there is any other problem in this design.... Thank You

Last edited by liligago; 12-12-2011 at 09:25 AM.
liligago is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, memory leak, memory management

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
14 members and 391 guests
7twenty7, AppsBlogger, Creativ, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, sacha1996, Sami Gh, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0