gpt4 book ai didi

iphone - IOS 上 UITableView 的 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-12-03 21:09:09 30 4
gpt4 key购买 nike

滚动表格时,我的应用程序崩溃,控制台显示这是EXC_BAD_ACCESS。我到处寻找,人们建议我在可执行文件环境变量上使用 NSZombieEnabled 。我已在可执行文件上将 NSZombieEnabledNSDebugEnabledMallocStackLoggingMallocStackLoggingNoCompact 设置为 YES。

但显然我仍然无法弄清楚程序的哪一部分导致了 EXC_BAD_ACCESS。这就是我的控制台所说的

[Session started at 2010-12-21 21:11:21 +0700.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1510) (Wed Sep 22 02:45:02 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 9335.
TwitterSearch(9335) malloc: recording malloc stacks to disk using standard recorder
TwitterSearch(9335) malloc: process 9300 no longer exists, stack logs deleted from /tmp/stack-logs.9300.TwitterSearch.suirlR.index
TwitterSearch(9335) malloc: stack logs being written into /tmp/stack- logs.9335.TwitterSearch.tQJAXk.index
2010-12-21 21:11:25.446 TwitterSearch[9335:207] View Did Load
Program received signal: “EXC_BAD_ACCESS”.

这是我尝试在 gdb 上输入 backtrace 时的情况:

Program received signal:  “EXC_BAD_ACCESS”.
(gdb) backtrace
#0 0x00f20a67 in objc_msgSend ()
#1 0x0565cd80 in ?? ()
#2 0x0033b7fa in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
#3 0x0033177f in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
#4 0x00346450 in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] ()
#5 0x0033e538 in -[UITableView layoutSubviews] ()
#6 0x01ffc451 in -[CALayer layoutSublayers] ()
#7 0x01ffc17c in CALayerLayoutIfNeeded ()
#8 0x01ff537c in CA::Context::commit_transaction ()
#9 0x01ff50d0 in CA::Transaction::commit ()
#10 0x020257d5 in CA::Transaction::observer_callback ()
#11 0x00d9ffbb in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#12 0x00d350e7 in __CFRunLoopDoObservers ()
#13 0x00cfdbd7 in __CFRunLoopRun ()
#14 0x00cfd240 in CFRunLoopRunSpecific ()
#15 0x00cfd161 in CFRunLoopRunInMode ()
#16 0x01a73268 in GSEventRunModal ()
#17 0x01a7332d in GSEventRun ()
#18 0x002d642e in UIApplicationMain ()
#19 0x00001d4e in main (argc=1, argv=0xbfffee34) at /Users/suprie/Documents/Projects/Self/cocoa/TwitterSearch/main.m:14

我真的很感谢任何帮助我调试我的应用程序的线索。

编辑这是表的头文件

#import <UIKit/UIKit.h>


@interface TwitterTableViewController : UITableViewController {

NSMutableArray *twitters;

}

@property(nonatomic,retain) NSMutableArray *twitters;

@end

和实现文件

#import "TwitterTableViewController.h"

@implementation TwitterTableViewController

@synthesize twitters;

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [twitters count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 90.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

const NSInteger TAG_IMAGE_VIEW = 1001;
const NSInteger TAG_TWEET_VIEW = 1002;
const NSInteger TAG_FROM_VIEW = 1003;

static NSString *CellIdentifier = @"Cell";

UIImageView *imageView;
UILabel *tweet;
UILabel *from;


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

// Image
imageView = [[[[UIImageView alloc] initWithFrame:CGRectMake(5.0f, 5.0f, 60.0f, 60.0f)] autorelease] retain];
[cell.contentView addSubview:imageView];
imageView.tag = TAG_IMAGE_VIEW;

// Tweet
tweet = [[[UILabel alloc] initWithFrame:CGRectMake(105.0f, 5.0f, 200.0f, 50.0f)] autorelease];
[cell.contentView addSubview:tweet];
tweet.tag = TAG_TWEET_VIEW;
tweet.numberOfLines = 2;
tweet.font = [UIFont fontWithName:@"Helvetica" size:12];
tweet.textColor = [UIColor blackColor];
tweet.backgroundColor = [UIColor clearColor];

// From
from = [[[UILabel alloc] initWithFrame:CGRectMake(105.0f, 55.0, 200.0f, 35.0f)] autorelease];
[cell.contentView addSubview:from];
from.tag = TAG_FROM_VIEW;
from.numberOfLines = 1;
from.font = [UIFont fontWithName:@"Helvetica" size:10];
from.textColor = [UIColor blackColor];
from.backgroundColor = [UIColor clearColor];
}

// Configure the cell...
NSMutableDictionary *twitter = [twitters objectAtIndex:(NSInteger) indexPath.row];
// cell.text = [twitter objectForKey:@"text"];

tweet.text = (NSString *) [twitter objectForKey:@"text"];
tweet.hidden = NO;

from.text = (NSString *) [twitter objectForKey:@"from_user"];
from.hidden = NO;

NSString *avatar_url = (NSString *)[twitter objectForKey:@"profile_image_url"];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: avatar_url]];

imageView.image = [UIImage imageWithData: imageData];
imageView.hidden = NO;


return cell;
}




#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableDictionary *twitter = [twitters objectAtIndex:(NSInteger)indexPath.row];
NSLog(@"Twit ini kepilih :%@", [twitter objectForKey:@"text"]);
}


#pragma mark -
#pragma mark Memory management

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

- (void)viewDidUnload {
}


- (void)dealloc {
[super dealloc];
}


@end

这是来自 myViewController 的 viewDidLoad :

- (void)viewDidLoad {

NSLog(@"View Did Load");
[super viewDidLoad];

twitterTableController = [[TwitterTableViewController alloc] init];

responseData = [[NSMutableData data] retain];

UIView *mView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
mView.backgroundColor = [UIColor whiteColor];

theTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
theTableView.delegate = twitterTableController;
theTableView.dataSource = twitterTableController;

UIBarButtonItem *rightButton = [[[UIBarButtonItem alloc] initWithTitle:@"Refresh" style:UIBarButtonItemStyleDone target:self action:@selector(refreshResult)] autorelease];

self.navigationItem.rightBarButtonItem = rightButton;
// [mView addSubview:label];

self.view = mView;
[self.view addSubview:theTableView];

NSString *url = @"http://search.twitter.com/search.json?q=suprie";

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}

[更新]我尝试使用Instrument来定位问题。我的 friend 说,如果是僵尸,仪器会告诉我。问题是,当程序崩溃时,仪器没有说什么。

我还删除了所有自动释放。还是程序崩溃。我成功地使用了 malloc_history,结果是

(gdb) shell malloc_history 10433 0x0552bfc0
malloc_history Report Version: 2.0
Process: TwitterSearch [10433]
Path: /Users/suprie/Library/Application Support/iPhone Simulator/4.2/Applications/AF5C1948-C3DC-4680-9023- 4C3DFE8A5E13/TwitterSearch.app/TwitterSearch
Load Address: 0x1000
Identifier: TwitterSearch
Version: ??? (???)
Code Type: X86 (Native)
Parent Process: gdb-i386-apple-darwin [10435]

Date/Time: 2010-12-21 23:28:41.144 +0700
OS Version: Mac OS X 10.6.5 (10H574)
Report Version: 6

ALLOC 0x552bfc0-0x552bfd7 [size=24]: thread_18d9540 |start | main | UIApplicationMain | GSEventRun | GSEventRunModal | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | MultiplexerSource::perform() | URLConnectionClient::processEvents() | URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long) | URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long) | URLConnectionClient::_clientDidFinishLoading(URLConnectionClient::ClientConnectionEventQueue*) | _NSURLConnectionDidFinishLoading | -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] | -[TwitterViewController connectionDidFinishLoading:] | -[NSString(NSString_SBJSON) JSONValue] | -[SBJsonParser objectWithString:] | -[SBJsonParser scanValue:] | -[SBJsonParser scanRestOfDictionary:] | -[SBJsonParser scanValue:] | -[SBJsonParser scanRestOfArray:] | +[NSMutableArray arrayWithCapacity:] | -[__NSPlaceholderArray initWithCapacity:] | +[__NSArrayM __new::] | __CFAllocateObject2 | class_createInstance | _internal_class_createInstanceFromZone | calloc | malloc_zone_calloc

但我无法阅读它。我不明白哪一行导致我的应用程序崩溃

[已修复]我已经修复了错误..这是新的 CellforRowatIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

const NSInteger TAG_IMAGE_VIEW = 1001;
const NSInteger TAG_TWEET_VIEW = 1002;
const NSInteger TAG_FROM_VIEW = 1003;
static NSString *CellIdentifier = @"Cell";

UIImageView *imageView;

UILabel *lblTweet;
UILabel *from;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0f, 5.0f, 60.0f, 60.0f)];
[cell.contentView addSubview:imageView];
imageView.tag = TAG_IMAGE_VIEW;

// Tweet
lblTweet = [[UILabel alloc] initWithFrame:CGRectMake(105.0f, 5.0f, 200.0f, 50.0f)] ;
[cell.contentView addSubview:lblTweet];
lblTweet.tag = TAG_TWEET_VIEW;
lblTweet.numberOfLines = 2;
lblTweet.font = [UIFont fontWithName:@"Helvetica" size:12];
lblTweet.textColor = [UIColor blackColor];
lblTweet.backgroundColor = [UIColor clearColor];

// From
from = [[UILabel alloc] initWithFrame:CGRectMake(105.0f, 55.0, 200.0f, 35.0f)] ;
[cell.contentView addSubview:from];
from.tag = TAG_FROM_VIEW;
from.numberOfLines = 1;
from.font = [UIFont fontWithName:@"Helvetica" size:10];
from.textColor = [UIColor blackColor];
from.backgroundColor = [UIColor clearColor];
}else{
// I add this to fix the error
imageView = (UIImageView *) [[cell viewWithTag:TAG_IMAGE_VIEW] retain];
lblTweet = (UILabel *)[cell viewWithTag:TAG_TWEET_VIEW];
from = (UILabel *)[cell viewWithTag:TAG_FROM_VIEW];
}

NSMutableDictionary *twitter = [twitters objectAtIndex:(NSInteger) indexPath.row];
NSString *textTweet = (NSString *) [twitter objectForKey:@"text"];

lblTweet.text = textTweet;
lblTweet.hidden = NO;

from.text = (NSString *) [twitter objectForKey:@"from_user"];
from.hidden = NO;

NSString *avatar_url = (NSString *)[twitter objectForKey:@"profile_image_url"];

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: avatar_url]];

imageView.image = [UIImage imageWithData: imageData];
imageView.hidden = NO;
return cell;
}

不知何故,当我向上滚动时,应用程序重新绘制表格,并且由于之前绘制了单元格,因此表格和其他任何内容都未正确初始化并抛出“EXC_BAC_ACCESS”。

非常感谢您的帮助:D

最佳答案

EXC_BAD_ACCESS 可能是由 AutoRelease 池尝试释放已显式释放的内容引起的。

在无法看到您的代码的情况下,我会查看您在代码中发布的内容( [something release]; ),并查看您是否确实需要发布它。如果您发现可疑的内容,请将其注释掉并再次运行您的应用程序。如果您遇到同样的错误,您就知道不是这样的。

关于iphone - IOS 上 UITableView 的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4500217/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com