0

几天来我一直在用头撞墙,需要一些帮助。我有一种感觉,我在这里做了一些非常愚蠢的事情,但我找不到问题。这是表格视图的控制器。作为此错误故障排除的一部分,我将 SQL 放入行中以简化它。通常,它将位于模型类的访问器方法中。

它通过 SQL 读取就好了。找到这两个对象,将它们加载到 todaysWorkout 数组中,然后为表格视图构建单元格。表格视图实际上出现在屏幕上,然后抛出 EXC_BAD_ACCESS。

我运行了仪器,它显示以下内容:

0 CFString Malloc 1 00:03.765 0x3946470 176 基础-[NSPlaceholderString initWithFormat:locale:arguments:]

1 CFString 自动释放 00:03.765 0x3946470 0 基金会 NSRecordAllocationEvent

2 CFString CFRelease 0 00:03.767 0x3946470 0 带上它-[WorkoutViewController viewDidLoad]

3 CFString Zombie -1 00:03.917 0x3946470 0 基金会 NSPopAutoreleasePool

这是控制器的源代码。我把它都留在了那里,以防万一有什么无关紧要的东西导致了问题。我真诚地感谢我能得到的任何帮助:

标题:

#import <UIKit/UIKit.h>
#import <sqlite3.h>
#import "NoteCell.h"
#import "BIUtility.h"
#import "Bring_ItAppDelegate.h"
#import "MoveListViewController.h"


@class MoveListViewController;
@class BIUtility;

@interface WorkoutViewController : UITableViewController {
    NSMutableArray *todaysWorkouts;
    IBOutlet NoteCell *woNoteCell;
    MoveListViewController *childController;
    NSInteger scheduleDay;
    BIUtility *bi;
}

@property (nonatomic, retain) NSMutableArray *todaysWorkouts;
@property (nonatomic, retain) NoteCell *woNoteCell;
@property (nonatomic,retain) BIUtility *bi;

//@property (nonatomic, retain) SwitchCell *woSwitchCell;

@end

班级:

#import "WorkoutViewController.h"
#import "MoveListViewController.h"
#import "Profile.h"

static sqlite3 *database = nil;

@implementation WorkoutViewController
@synthesize todaysWorkouts;
@synthesize woNoteCell;
@synthesize bi;

//@synthesize woSwitchCell;


- (void)viewDidLoad {
    [super viewDidLoad];


 bi = [[BIUtility alloc] init];


 todaysWorkouts = [[NSMutableArray alloc] init];

 NSString *query;
 sqlite3_stmt *statement;

 //open the database
 if (sqlite3_open([[BIUtility getDBPath] UTF8String], &database) != SQLITE_OK) {
  sqlite3_close(database);
  NSAssert(0, @"Failed to opendatabase");
 }

 query = [NSString stringWithFormat:@"SELECT IWORKOUT.WOINSTANCEID, IWORKOUT.WORKOUTID, CWORKOUTS.WORKOUTNAME FROM CWORKOUTS JOIN IWORKOUT ON IWORKOUT.WORKOUTID = CWORKOUTS.WORKOUTID AND DATE = '%@'", [BIUtility todayDateString]];
 if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
  while (sqlite3_step(statement) == SQLITE_ROW) {
   Workout *wo = [[Workout alloc] init];
   wo.woInstanceID = sqlite3_column_int(statement, 0);
   wo.workoutID = sqlite3_column_int(statement, 1);
   wo.workoutName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
   [todaysWorkouts addObject:wo];    
   [wo release];    
  }
  sqlite3_finalize(statement);

 } 
 if(database) sqlite3_close(database);

 [query release];




}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 //todaysWorkouts = [BIUtility todaysScheduledWorkouts];
 static NSString *noteCellIdentifier = @"NoteCellIdentifier";

 UITableViewCell *cell;


 if (indexPath.section < ([todaysWorkouts count])) {
  cell = [tableView dequeueReusableCellWithIdentifier:@"OtherCell"];
  if (cell == nil) {
   cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: @"OtherCell"] autorelease];
   cell.accessoryType = UITableViewCellAccessoryNone;
  } 
  if (indexPath.row == 0) {
   Workout *wo = [todaysWorkouts objectAtIndex:indexPath.section];   
   [cell.textLabel setText:wo.workoutName];
  } else {
   [cell.textLabel setText:@"Completed?"];
   [cell.textLabel setFont:[UIFont fontWithName:@"Arial" size:15]];
   [cell.textLabel setTextColor:[UIColor blueColor]];
  }

 } else { 
  cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:noteCellIdentifier];
  if (cell == nil) {
   NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NoteCell" owner:self options:nil];
   cell = [nib objectAtIndex:0];
  }
 }


 return cell; 

 //[cell release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSUInteger row = [indexPath row];

 if (indexPath.section < ([todaysWorkouts count]) && (row == 0)) {
  MoveListViewController *moveListController = [[MoveListViewController alloc] initWithStyle:UITableViewStylePlain];
  moveListController.workoutID = [[todaysWorkouts objectAtIndex:indexPath.section] workoutID];
  moveListController.workoutName = [[todaysWorkouts objectAtIndex:indexPath.section] workoutName];
  moveListController.woInstanceID = [[todaysWorkouts objectAtIndex:indexPath.section] woInstanceID];


  NSLog(@"Workout Selected: %@", [[todaysWorkouts objectAtIndex:indexPath.section] workoutName]);
  Bring_ItAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
  [delegate.workoutNavController pushViewController:moveListController animated:YES];
 } else {


  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
  if (indexPath.section < ([todaysWorkouts count]) && (row == 1)) {
   if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
   }
   else {
    cell.accessoryType = UITableViewCellAccessoryNone;
   }
  }
 }
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 NSInteger h = 35;
 return h;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return ([todaysWorkouts count] + 1);
 //return ([todaysWorkouts count]);

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 if (section < ([todaysWorkouts count])) {
  return 2;
 } else { 
  return 1;
 }


}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

 if (section < ([todaysWorkouts count])) {
  return @"Workout";
 } else { 
  return @"How Was Your Workout?";
 }

}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [todaysWorkouts release];
 [bi release];
 [super dealloc];
}


@end
4

3 回答 3

5

您正在释放query,但它被stringWithFormat方法标记为自动释放。

当运行循环再次释放它时,它已经被你的代码释放了,因此抛出了 EXC_BAD_ACCESS 异常。

仅释放您保留复制分配新建的对象。

于 2010-05-04T14:23:44.060 回答
0

罪魁祸首:

NSString *query;
query = [NSString stringWithFormat:@"SELECT IWORKOUT.WOINSTANCEID, IWORKOUT.WORKOUTID, CWORKOUTS.WORKOUTNAME FROM CWORKOUTS JOIN IWORKOUT ON IWORKOUT.WORKOUTID = CWORKOUTS.WORKOUTID AND DATE = '%@'", [BIUtility todayDateString]];
[query release];

query没有被保留,但你正在释放它。当自动释放池释放它时,你会偶然发现一个EXC_BAD_ACCESS过度释放的错误。

于 2010-05-04T14:26:24.270 回答
0

以下行创建了一个自动释放的 NSString,其保留计数为1

query = [NSString stringWithFormat:@"SELECT IWORKOUT.WOINSTANCEID, IWORKOUT.WORKOUTID, CWORKOUTS.WORKOUTNAME FROM CWORKOUTS JOIN IWORKOUT ON IWORKOUT.WORKOUTID = CWORKOUTS.WORKOUTID AND DATE = '%@'", [BIUtility todayDateString]]; 

当你这样做时

[query release];

的保留计数query将是0,因此它将被释放。但在自动释放池中仍有对其的引用。然后,当自动释放池稍后耗尽时,它会尝试执行

[query release];

也是。由于对象已经被释放并且指针现在无效,你得到EXC_BAD_ACCESS.

于 2010-05-04T14:26:27.800 回答