我一直在靠墙撞墙几天,需要一些帮助。我觉得我在做一些非常愚蠢的事情,但我找不到问题。这是表视图的控制器。我把SQL放在一行以简化它,作为此错误的故障排除的一部分。通常,它将位于模型类中的存取方法中。
它通过SQL读取就好了。查找这两个对象,将它们加载到todaysWorkout数组中,然后为表视图构建单元格。表视图实际上出现在scree上,然后抛出EXC_BAD_ACCESS。
我跑了乐器,它显示以下内容:
0 CFString Malloc 1 00:03.765 0x3946470 176基础 - [NSPlaceholderString initWithFormat:locale:arguments:]
1 CFString Autorelease 00:03.765 0x3946470 0 Foundation NSRecordAllocationEvent
2 CFString CFRelease 0 00:03.767 0x3946470 0带它 - [WorkoutViewController viewDidLoad]
3 CFString Zombie -1 00:03.917 0x3946470 0 Foundation NSPopAutoreleasePool
以下是控制器的源代码。我把它全部放在那里以防万一有什么东西导致问题。我真诚地感谢我能得到的任何帮助:
HEADER:
#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
CLASS:
#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
答案 0 :(得分:5)
您正在发布查询,但 stringWithFormat 方法将其标记为自动释放。
当运行循环再次释放时,它已经被您的代码释放,从而抛出了EXC_BAD_ACCESS异常。
仅发布保留,复制, alloc 或 new 的对象。
答案 1 :(得分: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
错误以进行过度释放。
答案 2 :(得分: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
。