objective-c 'double free' error in ARC mode - iphone

I'm trying to practice the block and GCD/NSOperation, so I wrote this project Async-objc, but when I'm trying to test it with unit test, I always encountered 'double free' error, I thought there shouldn't be such error in ARC mode.
here is the code
- (void)each:(NSArray *)items iterator:(callbackEach)iterator complete:(callbackWithError)complete {
NSBlockOperation *blockOp = [NSBlockOperation blockOperationWithBlock:^{
if (!items || items.count == 0) {
complete(nil);
return;
}
callbackWithError __block completeOnce = complete;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSLog(#"start operations %#", queue);
[queue setMaxConcurrentOperationCount:kMaxConcurrentOperationCount];
NSInteger __block count = 0;
for (id item in items) {
NSOperation *op = [NSBlockOperation blockOperationWithBlock:^{
iterator(item, ^(NSError *error) {
if (error) {
completeOnce(error);
completeOnce = nil;
} else {
count++;
if (count >= items.count) {
completeOnce(nil);
completeOnce = nil;
}
}
});
}];
[queue addOperation:op];
}
[queue waitUntilAllOperationsAreFinished];
NSLog(#"all operations are down %#", queue);
}];
[_mainQueue addOperation:blockOp];
}
this is the test code:
- (void)testEachWithBigData {
BOOL __block completed = false;
NSMutableArray *items = [NSMutableArray arrayWithCapacity:100];
for (int i = 1; i < 10000; ++i) {
[items addObject:[NSString stringWithFormat:#"%d", i]];
}
NSMutableArray *result = [NSMutableArray arrayWithCapacity:100];
[_async each:items
iterator:^(id item, callbackWithError callback) {
//NSLog(#"======item %#", item);
NSNumber *num = [NSNumber numberWithInt:[item integerValue]];
//NSLog(#"number %#", num);
[result addObject:num];
callback(nil);
}
complete:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
STAssertNil(error, #"there should be no error");
[result sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSNumber *num1 = (NSNumber *)obj1;
NSNumber *num2 = (NSNumber *)obj2;
if ([num1 integerValue] > [num2 integerValue]) {
return NSOrderedDescending;
} else if ([num1 integerValue] < [num2 integerValue]) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSInteger index = 1;
for (NSNumber *num in result) {
STAssertEquals(index, [num integerValue], #"the value should be ordered");
index++;
}
completed = YES;
});
}
];
NSDate *until = [NSDate dateWithTimeIntervalSinceNow:10];
while (!completed && [until timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:until];
}
}
If you couldn't reproduce the error, increase the test item count to 10000.
for (int i = 1; i < 10000; ++i) {
[items addObject:[NSString stringWithFormat:#"%d", i]];
}
and comment the NSLog()
then Command + U to run the unit test
here is the error message:
otest(11105,0xb039f000) malloc: * error for object 0xa0f8000: pointer being freed was not allocated
otest(11209,0xb0115000) malloc: * error for object 0x41dcc00: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
I cannot reproduce the 'double free' error this time

Related

UIApplicationWillEnterForegroundNotification not called

Hi I am very new in Iphone Development. I have a question :
I am using UIApplicationDeligate in my Appdeligate class and in this class i use two methods
applicationDidEnterBackground and applicationdisEnterForeGround. then these method works fine , when my app goes in background or in comes in foreground then flow goes inside these method.
But in another UiViewController class i attach listener for UIApplicationdidEnterForegroundNotification and UiApplicationdidEnterBackgroundNotification.
now when my app goes in background then flow goes in background method but when my app comes in foreground then app flow never comes in foreground method.
code which i am using in AppDelegate Class :
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:timertimetobefired forKey:#"TimerTimeToFired"];
[defaults setValue:timerrecipename forKey:#"TimerRecipeName"];
[defaults setObject:storingbuttonstate forKey:#"TimerButtonState"];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
timertimetobefired = [defaults objectForKey:#"TimerTimeToFired"];
timerrecipename = [defaults objectForKey:#"TimerRecipeName"];
storingbuttonstate = [defaults objectForKey:#"TimerButtonState"];
}
and code which i am using inside UiViewController class :
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:#"forground"
selector:#selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];
- (void) goBackground
{
if(timer != nil && [timer isValid])
{
[timer invalidate], timer = nil;
}
}
can anyone tell me what wrong with this code . Thanks in advance.
code inside doMylayoutStuff :
- (void)doMyLayoutStuff:(id)sender
{
fortimer=0;
appdelegate.imgRecipePhoto=nil;
recipeMakingTime=0;
arrForStepsReminder=[[NSMutableArray alloc]init];
[arrForStepsReminder removeAllObjects];
arrForSteps5=[[NSMutableArray alloc]init];
[arrForSteps5 removeAllObjects];
arrForButtonSelection=[[NSMutableArray alloc]init];
[arrForButtonSelection removeAllObjects];
[arrForSteps5 addObjectsFromArray:appdelegate.arrStepsandTime];
arrayforshowingdata = [[NSMutableArray alloc]initWithCapacity:arrForSteps5.count];
for (int i=0; i<[arrForSteps5 count]; i++)
{
//nitin tyagi. 6/08/2013.
// NSString *str12=[[arrForSteps5 objectAtIndex:i]objectForKey:#"RecipestepTime"];
// if (str12.length==0)
// {
// [arrForSteps5 removeObjectAtIndex:i];
// }
// else
// {
// }
NSString *timer1 = [[arrForSteps5 objectAtIndex:i]objectForKey:#"RecipeTimer1"];
NSString *timer2 = [[arrForSteps5 objectAtIndex:i]objectForKey:#"RecipeTimer2"];
NSString *timer3 = [[arrForSteps5 objectAtIndex:i]objectForKey:#"RecipeTimer3"];
NSString *timer4 = [[arrForSteps5 objectAtIndex:i]objectForKey:#"RecipeTimer4"];
timer1 = [timer1 stringByReplacingOccurrencesOfString:#" " withString:#""];
timer2 = [timer2 stringByReplacingOccurrencesOfString:#" " withString:#""];
timer3 = [timer3 stringByReplacingOccurrencesOfString:#" " withString:#""];
timer4 = [timer4 stringByReplacingOccurrencesOfString:#" " withString:#""];
NSMutableArray *finaltimerarray = [[NSMutableArray alloc]init];
int totalminute = 0;
int hours = 0;
if(([timer1 hasSuffix:#"mins"] || [timer1 hasSuffix:#"minute"] || [timer1 hasSuffix:#"min"]) || ([timer1 isEqualToString:#"0"]))
{
if(![timer1 isEqualToString:#"0"]){
NSRange range = [timer1 rangeOfString:#"min"];
NSString *minutes = [timer1 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"totalminute minute is = %#",totalminute);
}
else if(([timer1 hasSuffix:#"hours"] || [timer1 hasSuffix:#"hrs"] || [timer1 hasSuffix:#"hour"] || [timer1 hasSuffix:#"hr"]) )
{
if(![timer1 isEqualToString:#"0"]){
NSRange range = [timer1 rangeOfString:#"h"];
NSString *hours = [timer1 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer1 minute is = %#",minutes);
}
if(([timer2 hasSuffix:#"mins"] || [timer2 hasSuffix:#"minute"] || [timer2 hasSuffix:#"min"]) || ([timer2 isEqualToString:#"0"]))
{
if(![timer2 isEqualToString:#"0"]){
NSRange range = [timer2 rangeOfString:#"min"];
NSString *minutes = [timer2 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer2 minute is = %#",minutes);
}
else if(([timer2 hasSuffix:#"hours"] || [timer2 hasSuffix:#"hrs"] || [timer2 hasSuffix:#"hour"] || [timer2 hasSuffix:#"hr"]) )
{
if(![timer2 isEqualToString:#"0"])
{
NSRange range = [timer2 rangeOfString:#"h"];
NSString *hours = [timer2 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer1 minute is = %#",minutes);
}
if(([timer3 hasSuffix:#"mins"] || [timer3 hasSuffix:#"minute"] || [timer3 hasSuffix:#"min"]) || ([timer3 isEqualToString:#"0"]))
{
if( ![timer3 isEqualToString:#"0"])
{
NSRange range = [timer3 rangeOfString:#"min"];
NSString *minutes = [timer3 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer3 minute is = %#",minutes);
}
else if(([timer3 hasSuffix:#"hours"] || [timer3 hasSuffix:#"hrs"] || [timer3 hasSuffix:#"hour"] || [timer3 hasSuffix:#"hr"]) )
{
if(![timer3 isEqualToString:#"0"]){
NSRange range = [timer3 rangeOfString:#"h"];
NSString *hours = [timer3 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer1 minute is = %#",minutes);
}
if(([timer4 hasSuffix:#"mins"] || [timer4 hasSuffix:#"minute"] || [timer4 hasSuffix:#"min"]) || ([timer4 isEqualToString:#"0"]))
{
if(![timer4 isEqualToString:#"0"])
{
NSRange range = [timer4 rangeOfString:#"min"];
NSString *minutes = [timer4 substringToIndex:range.location];
totalminute = totalminute + [minutes intValue];
NSNumber* xWrapped = [NSNumber numberWithInt:[minutes intValue]];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer4 minute is = %#",minutes);
}
else if(([timer4 hasSuffix:#"hours"] || [timer4 hasSuffix:#"hrs"] || [timer4 hasSuffix:#"hour"] || [timer4 hasSuffix:#"hr"]) )
{
if(![timer4 isEqualToString:#"0"]){
NSRange range = [timer4 rangeOfString:#"h"];
NSString *hours = [timer4 substringToIndex:range.location];
int minute = [hours intValue] * 60;
totalminute = totalminute + minute;
NSNumber* xWrapped = [NSNumber numberWithInt:minute];
[finaltimerarray addObject:xWrapped];
}
else
{
[finaltimerarray addObject:[NSNumber numberWithInt:0]];
}
// NSLog(#"timer1 minute is = %#",minutes);
}
[arrayforshowingdata addObject:finaltimerarray];
[finaltimerarray release];
}
for (int i=0; i<[arrayforshowingdata count]; i++)
{
NSMutableArray *arraydata = [arrayforshowingdata objectAtIndex:i];
for (int j=0; j<[arraydata count]; j++)
{
[arrForStepsReminder addObject:#"0"];
}
[arrForButtonSelection addObject:#"M"];
}
lblDisplayTimer.text=#"00:00:00";
lblRecipeName.text=appdelegate.strRecipeName;
currentdate = [NSDate date];
NSDate *timetobefired = appdelegate.timertimetobefired;
timerrecipe = appdelegate.timerrecipename;
if(timerrecipe != nil && ([timerrecipe isEqualToString:appdelegate.strRecipeName]) && timetobefired != nil)
{
NSTimeInterval secondsBetween = [timetobefired timeIntervalSinceDate:currentdate];
NSInteger time = round(secondsBetween);
int second = (int)time;
if(second > 0)
{
secondsLeft = second;
arrForButtonSelection = appdelegate.storingbuttonstate;
[btnOnOff setOn:YES];
if(![timer isValid])
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateCountdown) userInfo:nil repeats:YES];
}
}
else
{
secondsLeft = 0;
}
}
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(goBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];
}
Change the observer to "self" in your addObserver call
i.e
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(doMyLayoutStuff:)
name: UIApplicationWillEnterForegroundNotification
object:nil];

Multiple sharedInstance called failed

In my application i have called sharedinstance multiple time in multiple method deffinition ,
Here my code,
Method 1
-(void) showActionSheet:(id)sender forEvent:(UIEvent*)event
{
if(isQuantity==YES)
{
[[WebService sharedInstance] getQuantity:^(BOOL result)
{
if(result)
{
NSLog(#"success");
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
Quantity = [context fetchObjectsForEntityName:NSStringFromClass([GetQuantity class]) withSortColumn:nil withSortDescending:TRUE withPredicate:nil];
NSLog(#"array ->%#",Quantity);
isQuantity=NO;
}
}];
}
popoverController1 = [[TSPopoverController alloc]initWithContentViewController:tableViewController1];
popoverController1.cornerRadius = 5;
popoverController1.titleText = #"Quantity";
popoverController1.popoverBaseColor = [UIColor blackColor];
popoverController1.popoverGradient= NO;
[popoverController1 showPopoverWithTouch:event];
}
Method 2
-(void) showActionSheetw:(id)sender forEvent:(UIEvent*)events
{
if(isSize==YES)
{
[[WebService sharedInstance] getDimension:^(BOOL result)
{
if(result){
NSLog(#"success");
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
dime = [context fetchObjectsForEntityName:NSStringFromClass([Getdimension class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
NSLog(#"array ->%#",dime);
}
}];
}
popoverController2 = [[TSPopoverController alloc] initWithContentViewController:tableViewController2];
popoverController2.cornerRadius = 5;
popoverController2.titleText = #"Size";
popoverController2.popoverBaseColor = [UIColor blackColor];
popoverController2.popoverGradient= NO;
// popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController2 showPopoverWithTouch:events];
}
EDIT
- (void) getDimension:(void (^)(BOOL))handler
{
JBContainedURLConnection *connection = [[JBContainedURLConnection alloc]init ];
[connection initWithGETUrl:IP methodName:GETDIMENSION param:nil andCompletionHandler:^(JBContainedURLConnection *connection, NSError *error, NSString *urlString, NSDictionary *userInfo, NSData *response)
{
if(error)
{
NSLog(#"Error: %#", error);
handler(FALSE);
}
else
{
if(response == nil)
handler(FALSE);
else
{
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
NSArray *existingResults = [context fetchObjectsForEntityName:NSStringFromClass([Getdimension class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
for (NSManagedObject *obj in existingResults)
[context deleteObject:obj];
[[DataAccessLayer sharedInstance] saveContext];
id responseData = [self DictionaryFromResponse:response];
if(responseData == nil)
handler(FALSE);
else
{
NSLog(#"Dimension Response: %#", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
NSArray *data=[responseData objectForKey:#"GetDimensionResult"];
NSLog(#"GetDimensionResult :%#",data);
for( NSDictionary *dict in data){
Getdimension *userDetails = [Getdimension newObject];
[userDetails fillFromDictionary:dict];
}
[[DataAccessLayer sharedInstance] saveContext];
handler(TRUE);
}
} }
}];
}
- (void) getQuantity:(void (^)(BOOL))handler
{
JBContainedURLConnection *connection = [[JBContainedURLConnection alloc]init ];
[connection initWithGETUrl:IP methodName:GETQUANTITY param:nil andCompletionHandler:^(JBContainedURLConnection *connection, NSError *error, NSString *urlString, NSDictionary *userInfo, NSData *response)
{
if(error)
{
NSLog(#"Error: %#", error);
handler(FALSE);
}
else
{
if(response == nil)
handler(FALSE);
else
{
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
NSArray *existingResults = [context fetchObjectsForEntityName:NSStringFromClass([GetQuantity class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
for (NSManagedObject *obj in existingResults)
[context deleteObject:obj];
[[DataAccessLayer sharedInstance] saveContext];
id responseData = [self DictionaryFromResponse:response];
if(responseData == nil)
handler(FALSE);
else
{
NSLog(#"GetQuantityResult Response: %#", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
NSArray *data=[responseData objectForKey:#"GetQuantityResult"];
// NSLog(#"GetDimensionResult :%#",data);
for( NSDictionary *dict in data){
GetQuantity *userDetails = [GetQuantity newObject];
[userDetails fillFromDictionary:dict];
}
[[DataAccessLayer sharedInstance] saveContext];
handler(TRUE);
}
} }
}];
}
Instance method
+ (id)sharedInstance
{
#synchronized(self)
{
if (manager == nil)
manager = [[self alloc] init];
}
return manager;
}
-(id)init
{
if(self = [super init])
{
}
return self;
}
-(NSString *)NSStringFromDictionaryUsingJSON:(id)dictionary
{
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
return [writer stringWithObject:dictionary];
}
-(id)DictionaryFromResponse:(NSData *)response
{
NSString *responseBody = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
SBJsonParser *parser = [[SBJsonParser alloc]init];
return [parser objectWithString:responseBody error:nil];
}
sharedInstance only works one time,ie,. if i call any of the method first its worked,if calls other method second time app gets crashed.Can any one please help me to sort it out
I guess sharedInstance method is messy.
It should be
+ (id)sharedInstance
{
if (manager == nil)
manager = [[self alloc] init];
return manager;
}
Enjoy Programming !
Have you declared instance of your class as static,
Declare you class object as :
static ClassName *manager;
And the allocate the same object in your sharedInstance method.
The reason may be that the memory object was released but reference is still there in the memory.So when you execute the shared instance method it found a reference to !nil object and leads your application to crash.
This is singleton class feature of iOS(objective C)

-[CFString retain]: message sent to deallocated instance 0x215076c0

I know this subject has been discussed several times, but I cannot figure out what is causing this error message. I've been stuck here for a couple weeks now and its driving me crazy. The first part of the method sets up a match for 2-4 players. Once a match is found, it tries to add 3 and 4 players within a certain limit. Right now, the limit is just set to 3 seconds, but I envision it being much larger once it is ready for release.
I know this is a memory error somewhere in the matchmakerViewController code. When I comment out the code changing the scene, I still get the error. Can you show me what I've done wrong and how to fix it?
-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
{
NSLog(#"IN matchmakerViewController!");
NSLog(#"Found a match!");
//startDate = [[NSDate date] retain];
//NSArray * playerIds1 = match.playerIDs;
//[marray_players addObject:playerIds1];
// Setup match
TXGameCenterManager *gameCenterManager = [TXGameCenterManager sharedTXGameCenterManager];
gameCenterManager.multiplayerMatch = match;
// The delegate of the match is HelloWorldLayer
gameCenterManager.multiplayerMatch.delegate = self;
AppDelegate * delegate = (AppDelegate *) [UIApplication sharedApplication].delegate;
[delegate.viewController dismissModalViewControllerAnimated:NO];
GKMatchRequest * matchRequest = [[[GKMatchRequest alloc] init] autorelease];
NSLog(#"playerIDs count is %d",[match.playerIDs count] );
if( match.expectedPlayerCount==0 )
{
// Launching the game without waiting for connection change messages
NSLog(#"Begin game without waiting for match connection change messages");
// Determine the host, local or remote
NSArray * playerIds = match.playerIDs;
NSLog(#"Number of players: %d", [playerIds count]);
NSLog(#"ID of player: %#", [playerIds lastObject]);
NSLog(#"I got the player ids");
[GKPlayer loadPlayersForIdentifiers:playerIds withCompletionHandler:^(NSArray *players, NSError * error)
{
NSLog(#"Player 1 %#", [[players objectAtIndex:0] alias] );
// NSLog(#"Player 2 %#", [[players objectAtIndex:1] alias] );
if( !error)
{
NSLog(#"Local player: %#", [[GKLocalPlayer localPlayer] alias]);
NSLog(#"Remote player: %#", [[players lastObject] alias]);
NSLog(#"Remote player: %#", [[players lastObject] alias]);
//NSArray *nsarray_player2 = [marray_players objectAtIndex:1];
// remote1name= [[nsarray_player1 lastObject] alias];
int_totalremoteplayers=[match.playerIDs count];
boo_playersfound=true;
localName = [[GKLocalPlayer localPlayer] alias];
remoteName = [[players objectAtIndex:0] alias];
NSLog(#"Player 1 %#", [[players objectAtIndex:0] alias] );
// NSLog(#"Player 2 %#", [[players objectAtIndex:1] alias] );
//remote1name= [[nsarray_player1 lastObject] alias];
if ([match.playerIDs count]==1)
{
NSLog(#"IN [match.playerIDs count]==1");
remoteplayer1name= [[players objectAtIndex:0] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:localName];
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"Host: %#", [sortedArray objectAtIndex:0]);
NSLog(#"Player1: %#", [sortedArray objectAtIndex:1]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
}
if ([match.playerIDs count]==2)
{
NSLog(#"IN [match.playerIDs count]==2");
NSLog(#" [match.playerIDs count]==2 Player 1 %#", [[players objectAtIndex:0] alias] );
NSLog(#"[match.playerIDs count]==2 Player 2 %#", [[players objectAtIndex:1] alias] );
NSLog(#"[match.playerIDs count]==2 localName %#", localName );
remoteplayer1name= [[players objectAtIndex:0] alias];
remoteplayer2name= [[players objectAtIndex:1] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:remoteplayer2name];
[playernames addObject:localName];
NSLog(#"Players in playernames %d", [playernames count]);
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"Host: %#", [sortedArray objectAtIndex:0]);
NSLog(#"Player1: %#", [sortedArray objectAtIndex:1]);
NSLog(#"Player2: %#", [sortedArray objectAtIndex:2]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
remoteplayer2name=[sortedArray objectAtIndex:2];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
NSLog(#"match.expectedPlayerCount = %d",match.expectedPlayerCount);
}
else if ([match.playerIDs count]==3)
{
remoteplayer1name= [[players objectAtIndex:0] alias];
remoteplayer2name= [[players objectAtIndex:1] alias];
remoteplayer3name= [[players objectAtIndex:2] alias];
[playernames addObject:remoteplayer1name];
[playernames addObject:remoteplayer2name];
[playernames addObject:remoteplayer3name];
[playernames addObject:localName];
NSArray *sortedArray;
sortedArray = [playernames sortedArrayUsingSelector:
#selector(localizedCaseInsensitiveCompare:)];
NSLog(#"Host: %#", [sortedArray objectAtIndex:0]);
NSLog(#"Player 1: %#", [sortedArray objectAtIndex:1]);
NSLog(#"Player 2: %#", [sortedArray objectAtIndex:2]);
NSLog(#"Player 3: %#", [sortedArray objectAtIndex:3]);
hostName=[sortedArray objectAtIndex:0];
remoteplayer1name=[sortedArray objectAtIndex:1];
remoteplayer2name=[sortedArray objectAtIndex:2];
remoteplayer3name=[sortedArray objectAtIndex:3];
if ([hostName isEqualToString:localName])
{
[[TXGameCenterManager sharedTXGameCenterManager] setHost:YES];
}
}
NSLog(#"Player names");
// TODO: Set the correct player names in the globals
player1 = localName;
player2 = [[NSUserDefaults standardUserDefaults] objectForKey:#"player2name"];
player3 = [[NSUserDefaults standardUserDefaults] objectForKey:#"player3name"];
player4 = [[NSUserDefaults standardUserDefaults] objectForKey:#"player4name"];
NSLog(#"Launch the game");
// Launch the game
int_team2tally=0;
NSLog(#"boo_startgame");
boo_startgame=true;
NSLog(#"go to the required scene");
//go to the required scene
}
else
{
NSLog(#"Error");
}
NSLog(#"StartMultiplayerGameTablehost");
//gameCenterManager.matchController.matchmakerDelegate = nil;
[self performSelector:#selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];
}
];
NSLog(#"No Error");
}
// Believe the error is somewhere in the following code.
if (boo_gamestarted!=true)
{
if (match.expectedPlayerCount==0)
{
//if([localName isEqualToString:hostName])
// {
boo_startgame=true;
if ([match.playerIDs count]==1)
{
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(#"An error occurred during adding a player to match: %#", [error localizedDescription]);}
else if (match != nil)
{NSLog(#"A player has been added to the match");}
}];
}
else if ([match.playerIDs count]==2)
{
matchRequest.minPlayers = 4;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(#"An error occurred during adding a player to match: %#", [error localizedDescription]);}
else if (match != nil)
{NSLog(#"A player has been added to the match");}
}];
}
if ([match.playerIDs count]==3)
{
NSLog(#"Starting Game-589");
// [CCTimer timerWithTarget:self selector:#selector(StartMultiplayerGameTablehost) interval:10];
//[self performSelector:#selector(StartMultiplayerGameTablehost) withObject:nil afterDelay: 1.];
} // end if (match.expectedPlayerCount==0)
else if (boo_gamestarted==false)
{
NSLog(#"player count = %d",[match.playerIDs count]);
NSLog(#"Starting Game-597");
// [self performSelector:#selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:1.];
}
//[CCTimer timerWithTarget:self selector:#selector(StartMultiplayerGameTablehost) interval:10];
// [self performSelector:#selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];
// }
//
else
{
boo_startgame=true;
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
matchRequest.playersToInvite = nil;
[[GKMatchmaker sharedMatchmaker] addPlayersToMatch:match matchRequest:matchRequest completionHandler:^(NSError *error)
{
if (error)
{
NSLog(#"An error occurrred during adding a player to match: %#", [error localizedDescription]);}
else if (match != nil)
{NSLog(#"A player has been added to the match");}
}];
[self performSelector:#selector(StartMultiplayerGameTablehost) withObject:nil afterDelay:3.];
}
}
}
}
-(void)StartMultiplayerGameTablehost
{
boo_gamestarted=true;
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer node]];
}
-(void)dealloc
{
[super dealloc];
}
The end result is a message that looks like:
#0 0x32499468 in ___forwarding___ ()
#1 0x323f0f68 in __forwarding_prep_0___ ()
#2 0x31ffac5e in _Block_object_assign ()
#3 0x39614614 in __copy_helper_block_333 ()
#4 0x31ffa928 in _Block_copy_internal ()
#5 0x34a86694 in _dispatch_Block_copy ()
#6 0x34a8894e in dispatch_group_async$VARIANT$up ()
#7 0x39613c4e in -[GKConnectionInternal connectParticipantsWithConnectionData:withSessionInfo:] ()
#8 0x32c5e21c in -[GKMatch connectToPeersWithDictionaries:version:sessionToken:cdxTicket:] ()
#9 0x32c694b2 in __block_global_17 ()
#10 0x34a8711e in _dispatch_call_block_and_release ()
#11 0x34a864b6 in _dispatch_client_callout ()
#12 0x34a87dca in _dispatch_main_queue_callback_4CF$VARIANT$up ()
#13 0x3246af3a in __CFRunLoopRun ()
#14 0x323ddebc in CFRunLoopRunSpecific ()
#15 0x323ddd48 in CFRunLoopRunInMode ()
#16 0x3a1db2ea in GSEventRunModal ()
#17 0x39c992f8 in UIApplicationMain ()
#18 0x0009f370 in main ()
None of these are from my now code.
The answer ended up being that I needed to release
matchRequest.minPlayers = 3;
matchRequest.maxPlayers = 4;
Before re-defining their values.

Why application shows low memory warning during heap shot analysis?

I am developing one application in which recurring transactions are inserted in database.while i am doing heap shot analysis it shows low memory warning while inserting data.its inserting 1200 records approximately.The code for insertion is as below.
-(void)generateReccuringEntry:(int)tranId withAllDate:(BOOL)isAll
{
NSMutableArray *arrDates = [[NSMutableArray alloc] init] ;
NSArray *arr = [[_dicSaveData valueForKey:#"tran_repeatd"] componentsSeparatedByString:#" "];
int day;
if ([self getDayWithDayMonth:[arr objectAtIndex:1]] == 0) {
day = [self getDay:[arr objectAtIndex:1]];
}
else{
day = 1;
}
NSDate *startDate = [_dicSaveData valueForKey:#"tran_date"];
NSString *strEndDate = [NSString stringWithFormat:#"%#",[_dicSaveData valueForKey:#"tran_enddate"]];
NSDate *endDate = nil;
strEndDate = [strEndDate stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
if (![strEndDate stringIsEmpty]) {
endDate = [_dicSaveData valueForKey:#"tran_enddate"];
}
else {
//if end date not selected
endDate = [[startDate dateByAddingYear:1]lastOfYearDate];
}
//get start recurring day
NSString *strDay = [[arr lastObject] stringByReplacingOccurrencesOfString:#"()" withString:#""];
//check start recuring day selected or not
if ([strDay hasPrefix:#"("] && [strDay hasSuffix:#")"])
{
startDate = [self getStartDay:[arr lastObject] withStartingDate:startDate];
if ([startDate compare:endDate]==NSOrderedDescending || [startDate compare:endDate]==NSOrderedSame)
{
SafeRelease(arrDates);
return;
}
if (isAll)
{
NSError *error = nil;
NSString *strQuery = [NSString stringWithFormat:#"insert into recuring values (NULL,%i,%#,%#,'%#',%#,'%#','','%#','%#',%i,1,'%#',0)",tranId,[_dicSaveData valueForKey:#"cat_id"],[_dicSaveData valueForKey:#"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:#"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:#"categoryText"]];
strQuery = [strQuery stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
[NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
if (error) {
[AppDelegate showAlert:[error description] withTitle:#"Error!"];
}
}
else{
NSDate *currentDate = [[NSDate date] getDateWithDeviceTimeZone];
if ([startDate compare:currentDate]== NSOrderedDescending)
{
NSError *error = nil;
NSString *strQuery = [NSString stringWithFormat:#"insert into recuring values (NULL,%i,%#,%#,'%#',%#,'%#','','%#','%#',%i,1,'%#',0)",tranId,[_dicSaveData valueForKey:#"cat_id"],[_dicSaveData valueForKey:#"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:#"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:#"categoryText"]];
strQuery = [strQuery stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
[NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
if (error) {
[AppDelegate showAlert:[error description] withTitle:#"Error!"];
}
}
}
}
int index = 2;
if (day == 1) {
index = 1;
}
do {
if ([[arr objectAtIndex:index] isEqualToString:#"Week"])
{
startDate = [startDate dateByAddingWeek:day];
}
else if([[arr objectAtIndex:index] isEqualToString:#"Month"])
{
startDate = [startDate dateByAddingMonth:day];
}
else
{
startDate = [startDate dateByAddingDays:day];
}
if (isAll)
{
NSError *error = nil;
NSString *strQuery = [NSString stringWithFormat:#"insert into recuring values (NULL,%i,%#,%#,'%#',%#,'%#','','%#','%#',%i,1,'%#',0)",tranId,[_dicSaveData valueForKey:#"cat_id"],[_dicSaveData valueForKey:#"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:#"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:#"categoryText"]];
strQuery = [strQuery stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
[NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
if (error) {
[AppDelegate showAlert:[error description] withTitle:#"Error!"];
}
}
//add recuring for only for future dates
else
{
NSDate *currentDate = [[NSDate date] getDateWithDeviceTimeZone];
if ([startDate compare:currentDate]== NSOrderedDescending)
{
NSError *error = nil;
NSString *strQuery = [NSString stringWithFormat:#"insert into recuring values (NULL,%i,%#,%#,'%#',%#,'%#','','%#','%#',%i,1,'%#',0)",tranId,[_dicSaveData valueForKey:#"cat_id"],[_dicSaveData valueForKey:#"subcat_id"],_txtDesc.text,_txtAmount.text,sDate,[_dicSaveData valueForKey:#"tran_repeatd"],eDate,_flagForever,[_dicSaveData valueForKey:#"categoryText"]];
strQuery = [strQuery stringByReplacingOccurrencesOfString:#"(null)" withString:#""];
[NSNumber requestWithSynchronousExcuteQuery:strQuery withReturnningError:&error];
if (error) {
[AppDelegate showAlert:[error description] withTitle:#"Error!"];
}
}
}
} while ([startDate compare:endDate]==NSOrderedAscending );
SafeRelease(arrDates);
}
can anyone tell that what is the problem with this code.so that its consuming so much memory?
Update:I have converted app to ARC.but it still shows memory warning.
Highly recommend you convert (if you can) to ARC. The compiler is far more capable of inserting the most efficient retains/releases for you. Would also completely remove any need for anything like SafeRelease.

SQLITE Database Error 14

I'm developing a game where there are 4 buildings and in these buildings, there are 3 stages inside. The player can play the stages one by one, from one building to another, however, when they have finished one cycle (eg. going from building 1 stage 1 to building 4 stage 3) I keep getting this error where it's an error 14 (SQLITE_CANTOPEN) whenever the player clicks on another building. It keeps happening every single time I test the app. Can anyone help me? :S
edit: sorry, here's the code I used to open the window to access the game:
NSArray *bldgLevels = [[SQLiteManager sharedManager] fetchDataFromDatabase: MAIN_DATABASE
withQuery: SQL_STMT_GET_BUILDING_LEVELS
withValuesAndTypes: [NSArray arrayWithObjects:
[NSString stringWithFormat: #"%d", gameStateProxy.selectedArea], #"i", nil]];
NSLog(#"bldgLevels: %#", bldgLevels);
NSLog(#"getting player levels");
NSArray *playerLevels = [[SQLiteManager sharedManager] fetchDataFromDatabase: MAIN_DATABASE
withQuery: SQL_STMT_GET_PLAYER_UNLOCKED_LEVELS
withValuesAndTypes: [NSArray arrayWithObjects:
[NSString stringWithFormat: #"%d", gameStateProxy.playerId], #"i", nil]];
NSLog(#"playerLevels: %#", playerLevels);
int i = 0;
while (i < [bldgLevels count])
{
NSDictionary *bldgLevel = (NSDictionary *)[bldgLevels objectAtIndex: i];
int levelId = [(NSNumber *)[bldgLevel objectForKey: #"level_id"] intValue];
int indexOrder = [(NSNumber *)[bldgLevel objectForKey: #"index_order"] intValue];
data = [BldgLevelData createObject];
data.levelId = levelId;
data.levelName = (NSString *)[bldgLevel objectForKey: #"name"];
data.rescuedPtsRequired = [(NSNumber *)[bldgLevel objectForKey: #"rescued_required"] intValue];
data.minPtsToComplete = [(NSNumber *)[bldgLevel objectForKey: #"min_pts"] intValue];
data.lastPlayed = #"";
data.dateAdded = #"";
data.isNew = NO;
data.isCompleted = NO;
data.isLocked = YES;
// Group levels together by their index order
if (indexOrder == 1) { groupId++; }
data.groupId = groupId;
data.indexOrder = indexOrder;
int j = 0;
while (j < [playerLevels count])
{
NSDictionary *playerLevel = (NSDictionary *)[playerLevels objectAtIndex: j];
int playerLevelId = [(NSNumber *)[playerLevel objectForKey: #"level_id"] intValue];
if (levelId == playerLevelId)
{
data.rescuedHighScore = [(NSNumber *)[playerLevel objectForKey: #"rescued_highscore"] intValue];
data.rescuedScore = [(NSNumber *)[playerLevel objectForKey: #"rescued_score"] intValue];
data.lastPlayed = (NSString *)[playerLevel objectForKey: #"lastplayed"];
data.dateAdded = (NSString *)[playerLevel objectForKey: #"dateadded"];
data.isNew = ([(NSString *)[playerLevel objectForKey: #"lastplayed"] isEqualToString: #""]);
data.isCompleted = ([(NSNumber *)[playerLevel objectForKey: #"completed"] intValue] == 1);
data.isLocked = NO;
break;
}
j++;
}
if (indexOrder == 1)
{
list = [NSMutableArray arrayWithCapacity: 1];
groupData = [BldgLevelGroupData createObject];
groupData.groupId = groupId;
groupData.firstLevelId = levelId;
groupData.timeLimit = [(NSNumber *)[bldgLevel objectForKey: #"time_limit"] intValue];
groupData.list = list;
// Get SOS items
NSMutableArray *sosItems = [NSMutableArray arrayWithCapacity: 0];
//
NSLog(#"getting levelSOSItems");
NSLog(#"i: %d, j: %d", i, j);
NSLog(#"list: %#", list);
NSLog(#"levelId: %d", levelId);
NSArray *levelSOSItems = [[SQLiteManager sharedManager] fetchDataFromDatabase: MAIN_DATABASE
withQuery: SQL_STMT_GET_LEVEL_SOS
withValuesAndTypes: [NSArray arrayWithObjects:
[NSString stringWithFormat: #"%d", levelId], #"i", nil]];
NSLog(#"Levelsositems: %#", levelSOSItems);
NSLog(#"getting player sos Items");
NSArray *playerSOSItems = [[SQLiteManager sharedManager] fetchDataFromDatabase: MAIN_DATABASE
withQuery: SQL_STMT_GET_PLAYER_SOS
withValuesAndTypes: [NSArray arrayWithObjects:
[NSString stringWithFormat: #"%d", gameStateProxy.playerId], #"i",
nil]];
NSLog(#"playerSoSItems: %#", playerSOSItems);
j = 0;
while (j < [levelSOSItems count])
{
SOSItemData *sosItemData = [SOSItemData createObject];
sosItemData.itemId = [(NSNumber *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"sos_id"] intValue];
sosItemData.itemName = (NSString *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"name"];
sosItemData.desc = (NSString *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"desc"];
sosItemData.coins = [(NSNumber *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"coins_cost"] intValue];
sosItemData.cash = [(NSNumber *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"cash_cost"] intValue];
sosItemData.rescuedPtsRequired = [(NSNumber *)[(NSDictionary *)[levelSOSItems objectAtIndex: j] objectForKey: #"rescued_required"] intValue];
int k = 0;
while (k < [playerSOSItems count])
{
if ([(NSNumber *)[(NSDictionary *)[playerSOSItems objectAtIndex: k] objectForKey: #"sos_id"] intValue] == sosItemData.itemId)
{
sosItemData.qty = [(NSNumber *)[(NSDictionary *)[playerSOSItems objectAtIndex: k] objectForKey: #"qty"] intValue];
break;
}
k++;
}
//NSLog(#"%#", sosItemData);
[sosItems addObject: sosItemData];
j++;
}
groupData.sosItems = sosItems;
[groupsList addObject: groupData];
}
NSLog(#"%#", data);
[list addObject: data];
i++;
}
and here's the code I used to fetch the data from the database:
-(NSArray *) fetchDataFromDatabase:(NSString *)dbPath withQuery: (NSString *)sql withValuesAndTypes: (NSArray *)params
{
/*
dbPath - Name of database file
sql - SQL statement. Prepared statements are allowed to be used here (refer to params
params - Array containing data and its corresponding data type
*/
NSMutableArray *result = nil;
//make sure db is closed
NSLog(#"close database first!");
sqlite3_close(database);
NSLog(#"open database!");
int dbStatus = [self openDatabase: dbPath];
if (dbStatus == SQLITE_OK)
{
//int stmtStatus = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
int stmtStatus = [self prepareStatement: sql withParams: params];
if(stmtStatus == SQLITE_OK)
{
result = [NSMutableArray arrayWithCapacity: 1];
while(sqlite3_step(stmt) == SQLITE_ROW)
{
NSMutableDictionary *row = [NSMutableDictionary dictionaryWithCapacity: 1];
int columnCount = sqlite3_column_count(stmt);
int j = 0;
while (j < columnCount)
{
/*
Possible sqlite data types:
SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB, SQLITE_NULL
*/
if (sqlite3_column_type(stmt, j) == SQLITE_INTEGER)
{
[row setObject: [NSNumber numberWithInt: sqlite3_column_int(stmt, j)]
forKey: [NSString stringWithUTF8String: sqlite3_column_name(stmt, j)]];
}
else if (sqlite3_column_type(stmt, j) == SQLITE_FLOAT)
{
[row setObject: [NSNumber numberWithDouble: sqlite3_column_double(stmt, j)]
forKey: [NSString stringWithUTF8String: sqlite3_column_name(stmt, j)]];
}
else if (sqlite3_column_type(stmt, j) == SQLITE_TEXT)
{
[row setObject: [NSString stringWithFormat: #"%s", sqlite3_column_text(stmt, j)]
forKey: [NSString stringWithUTF8String: sqlite3_column_name(stmt, j)]];
}
else if (sqlite3_column_type(stmt, j) == SQLITE_NULL)
{
[row setObject: [NSNull null]
forKey: [NSString stringWithUTF8String: sqlite3_column_name(stmt, j)]];
}
j++;
}
[result addObject: row];
}
}
else { NSLog(#"stmt error: %d", stmtStatus); }
}
else if (dbStatus == 14)
{
NSLog(#"db error: %d", dbStatus);
NSLog(#"killing app.");
exit(0);
}
else { NSLog(#"db error: %d", dbStatus); }
//Even though the open call failed, close the database connection to release all the memory.
sqlite3_close(database);
return result;
}
sorry if it is wrong..... in your code you didn't finalize statement but useed stmt variable .before close DB ,we should finalize statement if we use statement.....
comment for finalize statement
sqlite3_finalize(Stmt);