5 Replies - 2471 Views - Last Post: 14 January 2012 - 04:15 AM

#1 AlbuquerqueApache  Icon User is offline

  • D.I.C Addict

Reputation: 49
  • View blog
  • Posts: 523
  • Joined: 21-February 10

Memory Management

Posted 12 January 2012 - 02:18 PM

Ok, so I keep getting a EXC_BAD_ACCESS error which I am guessing is alot like a seg fault error in C.

My book isn't real specific on some of these details, so I need some help.



#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSDateComponents *comps = [[NSDateComponents alloc ]init]; 
	[comps setYear:1984];
	[comps setMonth:7];
	[comps setDay:18];
	[comps setHour:0];
	[comps setMinute:0];
	[comps setSecond:0];
	
	NSCalendar *g =[[NSCalendar alloc ]init];
	NSDate *dateofbirth = [g dateFromComponents:comps];
	NSDate *now = [NSDate date];
				   
	double timeSince = [now timeIntervalSinceDate:dateofbirth ];
	
				   
	NSLog(@"your age %@", timeSince);
	
	
	
	[pool drain];
   
	
	
    return 0;
}





I think I am running into trouble because I am initializing and allocating memory within the pool. Is this correct?

Is This A Good Question/Topic? 0
  • +

Replies To: Memory Management

#2 AlbuquerqueApache  Icon User is offline

  • D.I.C Addict

Reputation: 49
  • View blog
  • Posts: 523
  • Joined: 21-February 10

Re: Memory Management

Posted 12 January 2012 - 02:36 PM

Ok, apparently it has something to do with my print command.

and I revised my code to this:


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSDateComponents *comps; 
	[comps setYear:1984];
	[comps setMonth:7];
	[comps setDay:18];
	[comps setHour:0];
	[comps setMinute:0];
	[comps setSecond:0];
	
	NSCalendar *g;
	NSDate *dateofbirth = [g dateFromComponents:comps];
	NSDate *now = [NSDate date];
				   
	double timeSince = [dateofbirth timeIntervalSinceDate: now ];
	
				   
	NSLog(@"your age %d ", timeSince);
	
	
	
	[pool drain];
   
	
	
    return 0;
}





and it says:

your age -2061656132


ermmmm... is that the normal output of

the timeSinceDate method?

This post has been edited by AlbuquerqueApache: 12 January 2012 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 AlbuquerqueApache  Icon User is offline

  • D.I.C Addict

Reputation: 49
  • View blog
  • Posts: 523
  • Joined: 21-February 10

Re: Memory Management

Posted 13 January 2012 - 09:37 AM

Ok, not that I have alot of helpful advice on this thread ( ;) ) but I figured out my problem is my comps object not holding any of the data values I assign it. I'm unsure of why, I just started Objective C so any advice from the Gurus will be much appreciated.

 

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSDateComponents *comps = [[NSDateComponents alloc] init]; 
	[comps setDay:1];
	[comps setMonth:7];
	[comps setYear:1984];
	
	
	
	NSLog(@"comps at this point is %Lf", comps);
	
	NSCalendar *g = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
	NSDate *dateofbirth = [g dateFromComponents:comps];
	NSDate *now = [NSDate date];
				   
	double timeSince = [dateofbirth timeIntervalSinceDate: now ];
	
	
	
	
				   
	NSLog(@"your age %d ", timeSince);
	
	
	
	[pool drain];
   
	
	
    return 0;
}






I am also still curious as to if it is permissible to use an autopool and still manually assign memory to objects. My compiler doesnt seem to care if I do it, but just curious.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,027
  • Joined: 08-August 08

Re: Memory Management

Posted 13 January 2012 - 11:27 AM

timeSince will not be a double. I'm not sure what it will be, either int or some date/time type.
Was This Post Helpful? 0
  • +
  • -

#5 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Memory Management

Posted 14 January 2012 - 03:45 AM

So this is what I was able to come up with... double timeSince should be "NSTimeInterval timeSince" and your results will be a negative float in seconds format. What I would do is type cast for int and multi by -1 and divide by 31556926 (seconds per year) to show your results as a true age.


Here is my code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSDate *dateofbirth = [[NSDate alloc]initWithString:@"1984-7-1 12:00:00 +0000"];
        NSTimeInterval myAge = [dateofbirth timeIntervalSinceNow];
        NSLog(@"Your age is: %i", (int)(myAge/31556926*-1)); 
    }
    return 0;
}


This post has been edited by schultz2146: 14 January 2012 - 03:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Memory Management

Posted 14 January 2012 - 04:15 AM

Here is your code... I miss spoke in previous post. The timeSince is a double and the answer to "is that the normal output of the timeSinceDate method? " Yes. Its in a negative seconds format. (difference between dateofbirth and now, in seconds, with sub-millisecond precision)


Richard

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSDateComponents *comps = [[NSDateComponents alloc] init]; 
        [comps setDay:1];
        [comps setMonth:7];
        [comps setYear:1984];
        
        NSCalendar *g = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDate *dateofbirth = [g dateFromComponents:comps];
        NSDate *now = [NSDate date];
        
        NSTimeInterval timeSince = [dateofbirth timeIntervalSinceDate: now ];

        NSLog(@"Your age is: %i", (int)(timeSince/31556926*-1)); 
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1