English 中文(简体)
B. 采用NSMutable方法 在另一项职能中规定: EXC_BAD_ACCESS
原标题:Calling methods of NSMutableSet in another function throws EXC_BAD_ACCESS

我 st着这种法典:

static NSMutableSet* test_set;

-(void)foo1
{
  test_set=[NSMutableSet setWithObject:[NSNumber numberWithInt:1]];
  NSLog(@"count:%d",[test_set count]);
}


-(void)foo2
{
  NSLog(@"pointer:%p",test_set);
  NSLog(@"count:%d",[test_set count]); // here I get EXC_BAD_ACCESS
}

I calling foo2 only after foo1. My debug out is like:

count:1
pointer:0x262790
Program received signal:  “EXC_BAD_ACCESS”.

What s wrong? __ Intresting note: it fails only when foo2 is calling in schedule.__ Sorry, I missed details. Both works perfect. Thank you all

最佳回答

您没有掌握被分配到<条码>测试_set的物体,这意味着该物体可在<条码>-foo2前处理。 一般来说,如果在采用某种方法后需要一个物体才能生存,那么你就应当拥有该物体,例如通过<代码>+alloc或-retain<>/code>:

-(void)foo1
{
  test_set=[[NSMutableSet alloc] initWithObjects:[NSNumber numberWithInt:1], nil];
  NSLog(@"count:%d",[test_set count]);
}

-(void)foo1
{
  test_set=[[NSMutableSet setWithObject:[NSNumber numberWithInt:1]] retain];
  NSLog(@"count:%d",[test_set count]);
}

The rules of taking and relinquishing ownership of objects are discussed in the Mem或y Management Programming Guide.

问题回答

You haven t retained test_set. The set returned by setWithObject: will be autoreleased. If you add

[test_set retain];

在从<条码>查询后: 载于<条码>foo1(),并添加

[test_set release];

至<代码>foo2(>>末,它应当工作。

或许应该读到Cocoa 。 《记忆管理方案规划指南》()。





相关问题
Windows Mobile 6 Emulator change storage?

How do i change the size of the Windows Mobile 6 Emulator. Its fixed at 32mb. I read this post: Increasing Windows Mobile 5 Emulator Storage But it only helps for the 5.0 version. Isnt there any way ...

CUDA Memory Allocation accessible for both host and device

I m trying to figure out a way to allocate a block of memory that is accessible by both the host (CPU) and device (GPU). Other than using cudaHostAlloc() function to allocate page-locked memory that ...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a ...

Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I m not so sure. My assumption is (a) that an object I get from a function (dateFromComponents: ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

Objective-C returning alloc d memory in a function == bad?

This is on the iPhone. So what if I have a function like - (SomeObject*)buildObject; Do I need to pass in a variable that I have already alloc d outside like - (void)assignObject(SomeObject** out);...

热门标签