实例变量不可访问

发布于 2024-10-09 13:48:40 字数 1614 浏览 9 评论 0原文

这里有严重的问题...如果我尝试 NSLog 自定义对象的实例变量,我将收到 ECX_BAD_ACCESS。在我的 ViewController 中调用以下函数,payload 保存从 url 中提取的字符串数据。

- (void) initVcardWithData:(NSString *)payload {
  NSLog(@"1. initVcardWithData");
  aVCard = [[vcardItem alloc] initWithPayload:payload];
  VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];
  [self presentModalViewController:aVCardViewController animated:YES];
  [aVCard release];
}

到目前为止,一切都很好。 initWithWithVCard 函数如下,theVCardtheVCardN 定义在 @implementation 中,并在 (.h) 中设置为 @property(非原子,retain):

-(id)initWithVCard:(vcardItem *)aVCard {
  if(self = [super init]) {
      theVCard = [aVCard retain];
      theVCardN = [theVCard.PersonName retain];
  }

  NSLog(@"---- vCardViewController :: initWithVcard :: FirstName: %@", theVCard.PersonName.FirstName);
  return self;
}

如果我在 ViewDidLoad 中访问我的 ViewController aVCardViewController 中的 theVCardN 对象,一切都像魅力一样。我用该对象的数据设置了一些标签。

如果我随后尝试从连接到视图中按钮的 IBAction 调用的函数内的 theVCardN 访问实例变量,我会在调试器控制台收到 EXC_BAD_ACCESS 错误。尝试从实例变量中提取数据的函数如下:

-(IBAction)addressbookButtonTapped {
NSLog(@"RETAIN COUNT FOR theVCard: %i", [theVCard retainCount]);
NSLog(@"RETAIN COUNT FOR theVCardN: %i", [theVCardN retainCount]);
NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);



//[self dismissModalViewControllerAnimated:YES];
}

在调用 NSLog 之前,theVCardN 的 RetainCounter 输出“1”。然后,NSLog 行在调试器控制台中返回 EXC_BAD_ACCESS。

有什么想法吗?

Serious Problem here... i'm getting ECX_BAD_ACCESS if i try to NSLog an instance variable of my custom object. Following Function is called in my ViewController, payload holds String Data which is pulled from a url.

- (void) initVcardWithData:(NSString *)payload {
  NSLog(@"1. initVcardWithData");
  aVCard = [[vcardItem alloc] initWithPayload:payload];
  VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];
  [self presentModalViewController:aVCardViewController animated:YES];
  [aVCard release];
}

So far so good. The initWithWithVCard function is as follows, theVCard and theVCardN are defined in @implementation and also set as a @property (nonatomic, retain) in (.h).:

-(id)initWithVCard:(vcardItem *)aVCard {
  if(self = [super init]) {
      theVCard = [aVCard retain];
      theVCardN = [theVCard.PersonName retain];
  }

  NSLog(@"---- vCardViewController :: initWithVcard :: FirstName: %@", theVCard.PersonName.FirstName);
  return self;
}

If i access the theVCardN object in my ViewController aVCardViewController within ViewDidLoad everything works like a charm. I set some labels with data from that object.

If i then try to access the instance variables from theVCardN within a function which is called from an IBAction which is connected to a button in View, i get an EXC_BAD_ACCESS error at the debugger console. The Function which tries to pull data from the instance variables is as follows:

-(IBAction)addressbookButtonTapped {
NSLog(@"RETAIN COUNT FOR theVCard: %i", [theVCard retainCount]);
NSLog(@"RETAIN COUNT FOR theVCardN: %i", [theVCardN retainCount]);
NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);



//[self dismissModalViewControllerAnimated:YES];
}

The RetainCounter for theVCardN right before calling NSLog outputs "1". The NSLog Line then returns EXC_BAD_ACCESS in Debugger Console.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2024-10-16 13:48:40

不要调用-retainCount。绝对保留计数是没有用的。

retainCount 返回对象的绝对保留计数。实际值将是一个实现细节,通常完全超出您的控制范围,因为系统框架可能会在内部执行许多操作,导致保留计数以您不期望的方式修改。

它对于调试毫无用处,并且有大量专门用于跟踪此类问题的工具。

首先,如果发生崩溃,就会有回溯。发布它。在这种情况下可能不是那么有趣,但是,仍然要始终查看回溯,以至少确认它在您认为的位置/方式崩溃。

从发布的证据来看,听起来 theVCardN.FirstName 要么被设置为垃圾,要么底层字符串已被过度释放。打开僵尸检测模式,看看是否是这种情况。由于它在 FirstName 上崩溃,因此显示与创建/存储 FirstName 相关的代码。

此外,实例变量和方法应始终以小写字母开头; PersonName 应该是 personName & FirstName 应为 firstName

Do not call -retainCount. Absolute retain counts are useless.

retainCount returns the absolute retain count of an object. The actual value will be an implementation detail that is very often completely out of your control as the system frameworks may do any number of things internally to cause the retain count to be modified in ways you don't expect.

It is useless for debugging and their are a wealth of tools that are specifically focused on tracking down these kinds of issues.

First, if there is a crash, there is a backtrace. Post it. Probably not that interesting in this case, but, still, always look to the backtrace to at least confirm that it is crashing where/how you think it is.

From the evidence posted, it sounds like theVCardN.FirstName is either set to garbage or the underlying string has been over-released. Turn on zombie detection mode and see if that is the case. Since it is crashing on FirstName, then show the code related to creating/storing the FirstName.

Also, instance variables and methods should always start with a lowercase letter; PersonName should be personName & FirstName should be firstName.

梦里兽 2024-10-16 13:48:40

也许我读错了代码或误解了你的类结构,但看起来你正在记录:

NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);

上面,你说它仍在工作,你正在记录:

theVCard.PersonName.FirstName

你错过了“PersonName”吗?这意味着您应该记录:

NSLog(@"Save to Adressbook: %@", theVCardN.PersonName.FirstName);

Maybe i'm reading the code wrong or misunderstanding your class structure, but it looks like you logging:

NSLog(@"Save to Adressbook: %@", theVCardN.FirstName);

Above, where you say it is still working, you are logging:

theVCard.PersonName.FirstName

Are you missing the "PersonName"? Meaning you should be logging:

NSLog(@"Save to Adressbook: %@", theVCardN.PersonName.FirstName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文