- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在我的 *.h 文件中声明我的数组:
@interface aViewController: UIViewController{ NSMutableArray *anArray; // You will need to later change this many times.}@end
我为我的 *.m 文件分配内存:
-(void) viewDidLoad{ anArray = [[NSMutableArray alloc] init];}
我点击一个测试按钮来加载我的数组(最终它需要加载不同的值每次点击):
anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
我在这里释放它:
-(void) dealloc{ [anArray release]; [super dealloc];}
这一切看起来还好吗?
因为当我稍后运行这段代码时它崩溃了:
NSLog(@"%d", [anArray count]);
不确定为什么简单的“NSLog() 和计数”会导致一切崩溃。
德克,
让我这样说吧:我对指针、数组、字符串和内存有巨大的误解。
我已经阅读了我能在上面找到的所有内容...但是(尚未)找到一个简单、清晰、易于理解的描述。
你能推荐一个吗? (希望少于 10 页阅读。)是否有解释只是这个主题的引用......并且从“你有 12 年的编码经验......但没有处理过分配内存或指针”的角度来看。)
所以变量名不是我引用对象的方式?那为什么要有呢?
我已经习惯了很多其他的语言:
myString = "this"myString = "that"myInt = 5myInt = 15
(还有什么可以更简单。)
在我看来,这是最简单的方法。(而且它似乎有效。但它真的是正确的吗?)
Don't alloc any memory for my NSMutableArray initially.Don't re-alloc any memory repeatedly. (When I change my array's values.)Don't release it repeatedly. (Before I change my array's values.)Don't release it when I exit the program.But:Always remember to use RETAIN when I initially assign (and repeatedly reassign) new values to my anArray variable.
You are not loading your array with anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil]; Instead, you are replacing it with a new instance, and worse: with an instance, whose reference is actually owned by some entity you do not control
哇。所以我可以有 20 个数组……都叫同一个名字:anArray……而且它们都不一样? (没有全局数组这样的东西吗?)
etc. In order to clear out old values, the method removeAllObject may be handy. There are also mutation methods, which may be used to add multiple values at once.
所以...首先我必须“删除所有对象”...然后我可以调用ONE 方法重新添加我所有的新值。
anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain]; instead of the alloc/init sequence.
哇。我认为如果不为其分配空间,就无法将任何内容存储在数组中。
If you really intend to replace the entire array, you may want to consider using properties
我如何使用属性来做到这一点?
做这样的事情的正确方法是什么:
> anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];> anArray = [NSMutableArray arrayWithObjects:@"four", @"five", @"six", nil];
就像我会做的那样:
x = 12;x = 24;
哇。我真的完全误解了关于字符串、数组和内存的一切。我认为“简单的方法”是分配一次……使用可变数组……随心所欲地改变它……然后释放一次。
The problem with doing so is that this new array isn't retained,
我认为旧数组会消失......并且可以使用新数组。(但我想不是。)
Further, you have a memory leak because you never freed the original array.
我在想旧数组不应该被释放...我还没有完成它...我只是想更改它以包含我的新值。 (但我想不是。)
but one is to use [anArray release];
我在想这会导致我释放我分配的内存...(但我猜不会)...然后我必须重新分配更多内存。 (但我想不是。)
anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain];
所以我必须“保留”它……所以它不会从我身下消失?(不确定为什么会这样。直到我在我最后的 dealloc 调用中告诉它...)
Another, probably more correct way to fix it would be to use the addObject: or addObjectsFromArray: NSMutableArray methods instead of constantly creating new arrays.
我只想创建ONE 数组...然后按我的意愿使用它。我从不想ADD 到数组。我想将其设置为我的新值。
最佳答案
你没有加载你的数组
anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];
相反,您正在用一个新实例替换它,更糟糕的是:用一个实例,其引用实际上由您无法控制的某个实体拥有(很可能是一个 NSAutoreleasePool
)。您正确拥有,由
[[NSMutableArray alloc] init]
丢失,会被泄露。
不是替换整个数组引用,而是改变你已经拥有的那个,使用例如 addObject:
like
[anArray addObject: @"one"]
[anArray addObject: @"two"]
等为了清除旧值,removeAllObject
方法可能很方便。还有变异方法,可用于一次添加多个值。
或者,您可以使用您已经使用的构造方法分配数组,但要小心保留它。在 viewDidLoad
中执行
anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain];
而不是 alloc
/init
序列。
如果你真的打算替换整个数组,你可能要考虑使用 properties而不是手动进行引用计数。
关于objective-c - 声明、分配、加载和释放 NSMutableArray 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2308053/
我有一个附加了 View Controller 的 AVAudioPlayer 实例。 @property (nonatomic, retain) AVAudioPlayer *previewAudi
我是java初学者。假设我声明了一个 Account 类型的变量 Account _account = new Account("Thomas"); 然后在其他地方我做了这样的事情: _account
我在我的应用程序中使用了 3 个 UIViewController,现在我想知道当我从另一个应用程序切换到另一个 UIViewController 时释放它们是否是一个好主意。显然,这将是隐藏的,当它
我分配了一个直接缓冲区: ByteBuffer directBuffer = ByteBuffer.allocateDirect(1024); 我读过: Deallocating Direct Buf
场景。我有一个图表,我可以使用右键单击来执行平移。这非常有效。然后我完美地添加了右键菜单。 问题。现在,即使在拖动操作完成后释放鼠标,也会显示右键菜单。 有没有办法在 Java Swing 或 Jav
我使用此代码获取 ABPerson 的姓氏 CFStringRef lastNameRef = ABRecordCopyValue((ABRecordRef)personRecordRef, kABP
目前,我们在基于 C 的嵌入式应用程序中使用 malloc/free Linux 命令进行内存分配/取消分配。我听说这会导致内存碎片,因为内存分配/取消分配会导致堆大小增加/减少,从而导致性能下降。其
当我尝试释放缓冲区时遇到问题。每次我尝试将缓冲区传递给释放方法时,都会发生段错误。 Valgrind 确认段错误位于 BufferDeallocate 方法中。 ==30960== Memcheck,
我想知道何时按下或释放修改后的键(Ctrl 或 Shift)。 基本上,用户可以在按下修改键的情况下执行多次击键,而我不想在它被释放之前执行一个操作(想想 Emacs 和 Ctrl + X + S).
我编写了一个相当大的网络应用程序。它运行良好一段时间,然后慢慢开始运行缓慢,因为 DOM 节点开始爬升到 80,000 - 100,000 左右。 所以我一直在 Chrome 开发工具控制台 (DCT
我知道在像 c 这样的语言中,我需要在分配内存后释放它。 (我来自 Java),对此我有几个问题: 当我在做的时候: int array[30]; (即创建一个大小为 30 个整数的数组)与
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to release pointer from boost::shared_ptr? Detach
我有一个可以从多个后台线程访问的类,可能同时访问。我无法复制该类,因为重新创建它的内容(处理或内存方面)可能很昂贵。 也有可能在后台处理仍在继续并访问该属性时替换了此类的属性。 目前我有定期的保留/释
这个问题是对: 的扩展链接-1:Creating an image out of the ios surface and saving it Link-2:Taking Screenshots fro
我有一个实例变量 NSMutableArray* searchResults。 首先,我初始化它: self.searchResults = [[NSMutableArray alloc] init]
如果我在堆上声明一些东西,比如 char *a=new char[1000] 并且主程序停止,如果没有 delete[]<,那么分配的内存会发生什么 调用?它保留在堆上还是自动释放? 最佳答案 就C+
在开发相机应用时,我遇到了一个异常,该异常仅在我切换到其他应用时发生(onPause() 用于我的应用)。 01-15 17:22:15.017: E/AndroidRuntime(14336): F
使用 JDK 1.8 编译时出现 maven 编译器错误 无法执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (de
将 BufferedImage 保存到磁盘(以释放内存)的最快方法是什么? 我的 Java 应用程序处理大量图像(每约 300 毫秒将图像加载到内存中)。大多数这些图像都会立即被丢弃 (gc),但每隔
使用 JDK 1.8 编译时出现 maven 编译器错误 未能在项目 DUMMY 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:3.8.1:
我是一名优秀的程序员,十分优秀!