Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:42:01+00:00 2026-05-14T22:42:01+00:00

My problem is this: I can’t seem to access the variable todaysDate from the

  • 0

My problem is this: I can’t seem to access the variable todaysDate from the numberForPlot or numberOfRecordsForPlot functions (see below for numberForPlot), but I can from anywhere else in the file.

The NSLog in the viewDidLoad works perfectly, the date is set correctly. If I access the variable from my own class functions, then that’s fine too and it works. However, when I try to access it from numberForPlot I get an error:

Program received signal: “EXC_BAD_ACCESS”.

In my header file, I have the following – note my class implements CPPlotDataSource.

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface ResultsGraphViewController : UIViewController <CPPlotDataSource> {
    NSManagedObjectContext *managedObjectContext;
    CPXYGraph *graph;
    NSMutableArray *eventsArray;
    NSDate *todaysDate;
}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *eventsArray;
@property (nonatomic, retain) NSDate *todaysDate;

- (void)getEvents;
- (void)configureGraph;

@end

In the implementation file, I have (relevant highlights only):

@synthesize managedObjectContext;
@synthesize eventsArray;
@synthesize todaysDate;

and

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setTitle:@"Results"];

    todaysDate = [NSDate date];
    NSLog(@"Set today's date to %@", todaysDate);
    [self getEvents];
    [self configureGraph];
}

and

-(NSNumber *)numberForPlot:(CPPlot *)plot 
 field:(NSUInteger)fieldEnum 
 recordIndex:(NSUInteger)index 
{ 
NSLog(@"%d events in the array.", [eventsArray count]);
NSLog(@"today's date is %@.", todaysDate);

...

}

(in the last two lines, above, the number of events in the array is output successfully, but the last line causes the error).

Any ideas as to why this is a problem, and how I can get around it? I imagine it’s something to do with being the CPPlotDataSource – how does this affect scoping?

Or do I just have an error in my code? All help much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:42:02+00:00Added an answer on May 14, 2026 at 10:42 pm

    The problem is that [NSDate date] returns an autoreleased object, which you don’t hold on to. It will hang around until the end of the current cycle of the run loop (why it doesn’t crash immediately in your first NSLog() statement), then it will be released. When you try to access it in -numberForPlot:, it has been released and your application crashes.

    To fix this, change the line in -viewDidLoad to read

    self.todaysDate = [NSDate date];
    

    You defined todaysDate to be a property with the retain attribute, so this will retain your date. Just remember to add a [todaysDate release] in your -dealloc method to prevent a leak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.