我正在开发的应用程序使用ARC.我希望它处理大型文件,因此我不是整个文件加载,而是使用NSFileHandle readDataOfLength方法加载数据块.它发生在循环中,重复直到整个文件被处理:
- (NSString*)doStuff { // called with NSInvocationOperation
// now we open the file itself
NSFileHandle *fileHandle =
[NSFileHandle fileHandleForReadingFromURL:self.path
error:nil];
...
BOOL done = NO;
while(!done) {
NSData *fileData = [fileHandle readDataOfLength: CHUNK_SIZE];
...
if ( [fileData length] == 0 ) done = YES;
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
根据剖析器,没有内存泄漏; 但是,我的应用程序在处理文件时会占用大量内存.我猜 - 自动释放只在我处理完文件后才出现.我可以修改它而无需切换到手动内存管理吗?
memory-management objective-c autorelease automatic-ref-counting
我已成为一个有块的偏执狂和创建保留周期的可能性.我正在使用UIAlertView该类的基于块的版本,它允许您使用块而不是委托方法.我使用了很多这些Alertviews,所以我经常调用实例方法,这些方法做了很多繁重的工作.
我在方法中进行的分配是否someInstanceMethod会导致保留周期?
(我使用ARC进行内存管理.)
__weak id weakSelf = self;
[doWorkAndThen:^{
[weakSelf someInstanceMethod];
}];
-(void) someInstanceMethod{
//will either of the assignments below cause a retain cycle?
self.iVar = @"data";
[self setIvar:@"data";
}
Run Code Online (Sandbox Code Playgroud) 在Xcode 4.4中,我似乎无法将编译器标志添加到单个源文件中.我试图从单个文件中删除ARC,但我无法弹出添加标志.双击或选择项目并按Enter键不起作用,也尝试选择多个并按下输入不起作用.我尝试过两个独立的项目,其中一个是我用Xcode 4.4创建的.是否有另一种手动将编译器标志添加到源文件的方法?(我没有旧版本的Xcode来查看它如何存储值[可能在pbxproj中]).
注意:MacBook Pro带Retina显示屏 - OSX 10.8
我需要一些帮助来解决这个内存泄漏问题.我正在使用ARC.
潜在的泄漏就在这条线上:
NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
Run Code Online (Sandbox Code Playgroud)
这是代码:
// === Check if the game should play sound === //
if (sound == YES) {
//==== PLAY THE LOOSING SOUND ====//
// Form a URL to the sound file, which in init using the Path
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"wrong2" ofType:@"wav"];
NSURL *aFileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
// Create a sound ID,
SystemSoundID myID;
// Register the sound
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)aFileURL, &myID) ;
// Play the …Run Code Online (Sandbox Code Playgroud) 我有一个ARC项目,我正在尝试添加AFNetworking.在目标的构建阶段中,我使用"-fno-objc-arc"标记标记了所有AFNetworking文件.但是我仍然收到很多编译器警告,例如"方法可能缺少[超级dealloc]调用","No'assign','copy'或'retain'属性指定,指定假定",这有时是后跟"默认属性'赋值'不适合非gc对象".
我见过的关于AFNetworking的所有例子都说,如果我设置编译器标志,文件应该编译时没有错误或警告.我错过了什么?
我想知道调用[[MyClass alloc] init]已经存在的(已分配的)对象是否会在ARC中造成泄漏?或者它是否为您管理发布和重新分配?
会做更好的事情:
myObject = nil;
myObject = [[MyClass alloc] init];
Run Code Online (Sandbox Code Playgroud)
哪个更合适?还有另一种方法可以做到这一点吗?
我有一个iVar NSMutableArray* _syncModels,在我的类的init方法中我初始化它就像_syncModels = [[NSMutableArray alloc] initWithCapacity:10];
在整个课程中,我正在添加和删除数组中的对象,但我从不重新初始化_syncModels.
所以我得到了这份崩溃报告-[NSConcreteData filterUsingPredicate:]: unrecognized selector sent to instance 0x23c740c0.
在象征之后,导致此崩溃的线是 [_syncModels filterUsingPredicate:[NSPredicate predicateWithFormat:@"NOT (dataManagerFieldRecord = nil)"]];
世界如何_syncModels从一个NSMutableArray变为一个NSConcreteData?
这种特殊的崩溃是罕见的,但我们正在获得更多这些,其中一些对象被解散并被发送一个它无法处理的消息.
我们的目标是在ARC下使用6.1.
编辑
我想出问题...我无意中增加self(该公司拥有的参考_syncModels)来从调度队列self的dealloc方法...所以这是一个存储器管理问题...
CGImageRef使用ARC 管理内存的规则有哪些?也就是说,有人可以帮我找到正确的文档吗?
我从照片库中获取图像并创建一个UIImage显示:
CGImageRef newImage = [assetRep fullResolutionImage];
...
UIImage *cloudImage = [UIImage imageWithCGImage:newImage scale:scale orientation:orientation];
Run Code Online (Sandbox Code Playgroud)
我需要做CGImageRelease(newImage)什么?
我得到内存警告,但它不似乎是我还没有发布对象的逐渐积累,我没有看到有任何仪器泄漏.我很困惑.
一些Cocoa和Cocoa Touch类声明它们的委托属性assign而不是weak,它强制类的用户nil输出属性dealloc
-(void)dealloc
{
self.imageScrollView.delegate = nil;
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
}
Run Code Online (Sandbox Code Playgroud)
这非常麻烦.
为什么苹果会这样做?
cocoa cocoa-touch delegates automatic-ref-counting declared-property
我有一个简单的视图控制器,其中有几个视图对象,我以编程方式创建.这是ViewController.h的一部分:
@property (nonatomic, strong) UIScrollView* scrollView;
@property (nonatomic, strong) UILabel* listingTitle;
@property (nonatomic, strong) MKMapView* listingMap;
@property (nonatomic, strong) UILabel* listingPrice;
Run Code Online (Sandbox Code Playgroud)
如您所见,它们都是强有力的参考.如果我将其中任何一个弱引用,ARC就会释放它们.据我所知,如果它们被添加到视图层次结构中,它们就不会解除分配,因为它们归层次结构中的下一级视图所有.对于其中一个,作为一个例子,我在这里这样做:
self.listingTitle = [[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width - 10, 60)];
[self.scrollView addSubview:listingTitle];
Run Code Online (Sandbox Code Playgroud)
但同样,如果我给它一个弱引用,它会解除分配.这是为什么?
objective-c ×6
ios ×4
cocoa-touch ×2
afnetworking ×1
autorelease ×1
cocoa ×1
delegates ×1
ios5 ×1
iphone ×1
memory-leaks ×1
xcode ×1