在ARC下面假设以下代码,
typedef void (^MyResponseHandler) (NSError *error);
@interface MyClass : NSObject
{
MyResponseHandler _ivarResponseHandler;
}
- (void)myMethod:(MyResponseHandler)responseHandler
{
_ivarResponseHandler = responseHandler;
...
}
Run Code Online (Sandbox Code Playgroud)
问题:当分配给ivar时,块是否自动复制到堆中?
我之前的问题暗示它是在通过a分配时复制的@property.但是,今天我使用了上面的代码并收到了EXC_BAD_ACCESS修改后修改的代码
_ivarResponseHandler = [responseHandler copy].
在关于Paul Hegarty课程的iOS 5教程之后,我们在一个演示使用GCD的应用程序中看到了这个代码.显然GCD需要发布,因为与多线程有关的问题在仍然需要什么对象方面有些不可预测.
dispatch_async(dowloadQueue, ^{
....
// do some stuff
....
});
dispatch_release(dowloadQueue);
Run Code Online (Sandbox Code Playgroud)
我已升级到iOS 6和XCode 4.5,我收到"ARC forbids explicit release ..."消息
我没有在iOS5/XCode 4.2下尝试这个代码
这只是因为ARC更聪明,我不再需要在GCD中释放?或者我只是误解了?
这会导致任何类型的保留周期吗?使用安全吗?
__block void (^myBlock)(int) = [^void (int i)
{
if (i == 0)
return;
NSLog(@"%d", i);
myBlock(i - 1);
} copy];
myBlock(10);
myBlock = nil;
Run Code Online (Sandbox Code Playgroud) recursion objective-c objective-c-blocks automatic-ref-counting
我的iOS应用程序具有高内存使用率但没有内存泄漏.如何减少内存使用量.
使用Instruments,我发现我的应用程序最大输出为90MB,在发生内存警告之前,其他内存被释放,然后在其余部分使用时保持在55-65MB左右.
我觉得55-65MB太高了吧?
因为,仪器没有发现任何泄漏.我该怎么做才能减少内存使用量?
我浏览了今年的WWDC视频,但是我理解的东西(这是我的第一个iOS应用程序),它主要涉及泄漏问题.
一些可能有用的信息:
VM:ImageIO_GIF_Data 30.35MB Live Bytes | 115生活| 300瞬态| 136.12 MB总字节数
VM:MappedFile 36.04 MB Live Bytes | 16生活| 11瞬态| 36.09 MB总字节数
所有其他的东西都在1MB以下
我的应用程序从互联网下载大约30个GIF文件,我使用SDWebImage,我只保存图像的URL,SDWebImage完成剩下的工作.:P
提前致谢,
来自iOS内存管理第一计时器
再次感谢您的帮助
cocoa-touch objective-c instruments ios automatic-ref-counting
EDIT2:
不.建议的答案是关于异步调用.我想要并需要同步调用,就像在普通的标准递归调用中一样.
编辑:
而
__unsafe_unretained void (^unsafe_apply)(UIView *, NSInteger) ;
Run Code Online (Sandbox Code Playgroud)
在没有警告或错误的情况下编译,它在运行时失败,并将NULL存储到unsafe_apply中.
不过这个:
- (void) applyToView: (UIView *) view {
UIColor * (^colorForIndex)(NSInteger) = ^(NSInteger index) {
return [UIColor colorWithHue: ((CGFloat) index / 255.0f)
saturation: 0.5f
brightness: 0.5f
alpha: 1.0f] ;
} ;
void (^applyColors) (UIView *, NSInteger index) = ^(UIView * view, NSInteger index) {
view.backgroundColor = colorForIndex(index) ;
} ;
void (^__block recurse_apply)(UIView *, NSInteger) ;
void (^apply)(UIView *, NSInteger) = ^(UIView * view, NSInteger level) { …Run Code Online (Sandbox Code Playgroud) recursion objective-c objective-c-blocks automatic-ref-counting retain-cycle
我正在编写一个简单的按钮类,如下所示:
@interface MyButton : NSObject {
id object;
SEL action;
}
@property(strong) id object;
@property SEL action;
-(void)fire;
@end
@implementation MyButton
@synthesize object, action;
-(void)fire {
[object performSelector:action];
}
@end
Run Code Online (Sandbox Code Playgroud)
我从Clang得到以下警告[object performSelector:action]:
PerformSelector may cause a leak because its selector is unknown
Run Code Online (Sandbox Code Playgroud)
经过一些研究后,我发现选择器可以属于具有不同内存要求的系列.目的是使行动失效,因此不应引起任何ARC困难并且应该适合none家庭.
它看起来像我想要的相关预处理器代码片段,或者是以下的变体:
__attribute__((objc_method_family(none)))
Run Code Online (Sandbox Code Playgroud)
但是我在哪里告诉Clang不要担心?
假设我有一个返回一堆自动释放的NSData对象的循环...
NSData* bigData = ...
while(some condition) {
NSData* smallData = [bigData subdataWithRange:...];
//process smallData
}
Run Code Online (Sandbox Code Playgroud)
在ARC下,我是否仍然应对@autoreleasepool这种while情况?
NSData* bigData = ...
@autoreleasepool {
while(some condition) {
NSData* smallData = [bigData subdataWithRange:...];
//process smallData
}
}
Run Code Online (Sandbox Code Playgroud)
我之所以问的原因是我看到我的NSData对象通过屋顶的工具中的生活分配数量调用dataWith...方法而不是initWith...方法.当我使用时initWith...,生活分配数量要少得多.
initWith...尽可能选择方法更好吗?
memory-management objective-c nsautoreleasepool automatic-ref-counting
在第二次显示弹出控制器时(在解除它然后重新显示它之后),我收到以下错误:
因未捕获的异常'NSGenericException'而终止应用程序,原因:' - [UIPopoverController dealloc]到达,而popover仍然可见.
堆栈跟踪只是一堆十六进制,SIGABRT每次都发生在UIApplicationMain.这是按钮触发的代码:
- (IBAction)createNewScore:(id)sender {
if (self.pc)
if (self.pc.popoverVisible)
return;
else
// Breakpoint is hit here—crashes after this line
[self.pc presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
NGDocumentInfoViewController *documentInfoVC = [[NGDocumentInfoViewController alloc] initWithBlankDocumentTargetInManagedObjectContext:self.context];
UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:documentInfoVC];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneCreatingNewScore:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelCreatingNewScore:)];
navc.navigationBar.topItem.leftBarButtonItem = doneButton;
navc.navigationBar.topItem.rightBarButtonItem = cancelButton;
CGSize popoverSize = CGSizeMake(documentInfoVC.view.bounds.size.width, documentInfoVC.view.bounds.size.height);
documentInfoVC.contentSizeForViewInPopover = popoverSize;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navc];
popover.delegate = self;
self.pc …Run Code Online (Sandbox Code Playgroud) memory-management dealloc uipopovercontroller ios automatic-ref-counting
我有一个看起来像这样的属性:
@property (weak, nonatomic) id<NavigationControllerDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的应用程序时,我收到以下错误:
objc[4251]: cannot form weak reference to instance (0x101e0d4b0) of class TabBarController
Run Code Online (Sandbox Code Playgroud)
我可以从谷歌获取此错误的唯一原因是,当您尝试形成对覆盖retain/release/dealloc的对象的弱引用时,您会得到它,我不是.我的TabBarController继承自NSViewController.
有谁知道这会导致什么?如果我使用"assign",它会起作用,但显然我更喜欢使用"weak".
由于Xcode的UIPageViewController模板缓存了所有页面数据,我遇到了一些内存问题,因此我将其更改为动态加载页面,所以现在当我的应用程序收到内存不足警告时,它会释放内存以便页面不显示,但如果用户通过点击屏幕边缘快速翻阅页面,它仍然会崩溃.我猜这是因为当调用didReceiveMemoryWarning时,它无法足够快地释放内存.如果用户缓慢翻转,它可以正常工作.我限制了用户翻页的速度,但它仍然会发生.我希望每次翻页时都能释放内存,而不必等待低内存警告.我正在使用ARC.有没有办法做到这一点?或者我还能做些什么来防止这种情况发生?谢谢.
编辑:
(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(SinglePageViewControllerSuperclass *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(SinglePageViewControllerSuperclass *)viewController];
if (index == NSNotFound || index == MAX_PAGE_INDEX) {
return nil;
}
return [self viewControllerAtIndex:++index];
}
Run Code Online (Sandbox Code Playgroud) objective-c ×7
ios ×5
recursion ×2
clang ×1
cocoa ×1
cocoa-touch ×1
dealloc ×1
instruments ×1
ivar ×1
macos ×1
retain-cycle ×1
xcode ×1