我正在开发一个目标C框架,最后将作为静态库提供.但是当我将该库集成到泄漏工具中的实际应用程序(通过添加静态库)时,我发现存在一些内存泄漏.
这是一个示例场景.
@implementation Test
@synthesize testNumber
+(Test) createTestInstance {
Test *test = [[Test alloc] init];
test.testNumber = [[NSDecimerNumber alloc] initWithInt:1];
return test;
}
-(void) dealloc {
[testNumber release];
}
@end
Run Code Online (Sandbox Code Playgroud)
虽然我在dealloc中发布了testNumber变量,但我在alloc位置看到Leaks工具中的内存泄漏.这可能是什么问题?
此外,由于这是一个供用户调用的库,从库代码中释放这些变量是最佳做法吗?
谢谢
一个哲学问题,各种各样.将常量字符串分配给(保留)的@property是否合适?或者,我应该这样做self.string = [NSString stringWithString:@""];
有内存泄漏吗?如果它被过度释放怎么办?
它是一个常量字符串,它的行为方式与NSString对象相同吗?
如果属性是(assign),那意味着它在运行循环后无效吗?
该方法retainCount应该返回无符号整数.
为什么然后,[@"Hi" retainCount]返回-1?
我正在检查一些对象的保留计数
NSLog(@"r = %d", [aObject retainCount];
Run Code Online (Sandbox Code Playgroud)
似乎我能得到的最低值是"r = 1",即使我故意添加额外的"释放"调用
[aObject release];
Run Code Online (Sandbox Code Playgroud)
即使我尝试将"释放"和"NSLog"测试代码放在对象的dealloc 方法中,"r = 1"限制仍然 有效.
Cocoa运行时似乎忽略了我的额外版本,直到"r = 1",然后在示例程序的最后使用"EXC_BAD_ACCESS"崩溃(没有GC).
我唯一的解释(猜测)是我们需要r> = 1来访问对象.并且Cocoa运行时只是试图避免让任何对象的保留计数过早地变为0.
如果我错了,有人可以确认或纠正我吗?
当我使用NSArray和NSString对象记录保留计数时,我的行为不均匀.
请参阅下面的代码,
NSArray *aryTemp = [NSArray arrayWithObjects:@"One",nil];
NSLog(@"Retain Count :%d",[aryTemp retainCount]);
NSArray *aryTemp2 = [[NSArray alloc] initWithObjects:@"One",nil];
NSLog(@"Retain Count :%d",[aryTemp2 retainCount]);
NSString *strTemp = @"One";
NSLog(@"Retain Count :%d",[strTemp retainCount]);
NSString *strTemp2 = [[NSString alloc] initWithString:@"One"];
NSLog(@"Retain Count :%d",[strTemp2 retainCount]);
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出
2011-03-01 19:19:32.410 Test[14607:207] Retain Count :37
2011-03-01 19:19:32.412 Test[14607:207] Retain Count :1
2011-03-01 19:19:32.413 Test[14607:207] Retain Count :2147483647
2011-03-01 19:19:32.413 Test[14607:207] Retain Count :2147483647
Run Code Online (Sandbox Code Playgroud)
代码有什么问题?
谢谢
Pratik Goswami
我正在尝试构建rss阅读器,当用户完成阅读artical并按回dealloc时不会调用的问题
我得到了retainCount 6和7次!
我有很多自定义面板
当按下后退按钮时,视图是poped并且没有dealloc被称为?!
.h文件:
@interface ArticalViewController : UIViewController<UIWebViewDelegate,UIScrollViewDelegate,UIActionSheetDelegate,ArticalBottomPanelDelegate,ArticalContentFetcherDelegate> {
UIWebView * description_;
UIActivityIndicatorView * ind_;
ArticalModel * artical_;
NSString * content;
UIButton * faceBookShareBtn_;
UIBarButtonItem * btnSharePanel_;
CustomTopToolBar * topToolbar_;
ArticalBottomPanel* articalBottomPanel_;
MovingSharePanel * movingSharePanel_;
int fontSize;
BOOL favoStatus;
ArticalContentFetcher *datafetcher_;
}
@property (nonatomic,retain) IBOutlet UIWebView * description;
@property (nonatomic,retain ) IBOutlet UIActivityIndicatorView * ind;
@property (nonatomic,retain) ArticalModel * artical;
@property (nonatomic,retain) IBOutlet UIButton * faceBookShareBtn;
@property (nonatomic,retain) IBOutlet CustomTopToolBar * topToolbar;
@property (nonatomic , retain) IBOutlet ArticalBottomPanel …Run Code Online (Sandbox Code Playgroud) 我正在将我的项目转换为使用ARC,并遇到了一个特殊问题.我有一个类来管理从网络下载的文件缓存.每个文件都存储在iPhone文件系统中,相关对象保存在我的管理器类中.其他想要使用文件的对象,请求我的经理获取缓存对象,并在需要该文件时保留该文件.
但偶尔,经理会清理缓存,删除旧文件.当然,它不应该删除当时正在使用的文件.在ARC之前,我通过使用关联对象的retainCount检测到:
// if retainCount is 1 then only the cache has a reference to it
if( obj.retainCount <= 1 ) {
[obj deleteFile];
[cache removeObject:obj];
}
Run Code Online (Sandbox Code Playgroud)
这完全有效[是的,我知道有关retainCount不可靠的警告,但根据我的经验,如果retainCount> 1,你肯定知道不止一个对象保留了它]
但是,使用ARC时,不允许再使用retainCount.我可以引入自己的保留计数机制,并要求所有使用文件的对象明确保留和释放文件对象.但那是错误的,而且这正是ARC应该解决的问题.
你知道更好的方法来实现同样的目标吗?
cocoa-touch objective-c retaincount ios5 automatic-ref-counting
在其他堆栈溢出问题中,强调捕获[weak self]应该用于不属于类的闭包,因为self在闭包完成之前可能是nil.当闭包属于类本身时,另一种选择是[unowned self].
我的问题是,[unowned self]当我作为参数传递的函数是当前类的实例方法时,我需要使用吗?
import RxSwift
class Person {
var name = "Default name"
class func getPersons() -> Observable<Person> {
// ...
}
}
class MyController: UIViewController {
let disposeBag = DisposeBag()
// I know this right
func unownedDisplayPeople() {
Person.getPersons()
.subscribeNext { [unowned self ] person in
self.displayName(person)
}
.addDisposableToBag(disposeBag)
}
// But what about this?
func whatAboutThisDisplayPeople() {
Person.getPersons()
.subscribeNext(displayName)
.addDisposableToBag(disposeBag)
}
// Or this?
func …Run Code Online (Sandbox Code Playgroud) 我想我知道Swift上的保留周期是什么以及它为什么会产生内存泄漏.但我写了一个小例子来演示它,似乎代码正确地解除了分配.
在这个例子中,我有两个相互保留的对象(创建保留周期),第三个对象强有力地保持两者.
我希望第三个对象也不可能解除分配,但事实并非如此.
相互保留的两个对象:
class Obj1: NSObject {
var objc2: Obj2?
deinit {
print("Obj1 Deinit")
}
}
class Obj2: NSObject {
var obj1: Obj1?
deinit {
print("Obj2 Deinit")
}
}
Run Code Online (Sandbox Code Playgroud)
容器:
class Container {
var obj1: Obj1?
var obj2: Obj2?
deinit {
print("Container Deinit")
}
}
Run Code Online (Sandbox Code Playgroud)
生成保留周期的代码
let obj1 = Obj1()
let obj2 = Obj2()
obj1.objc2 = obj2
obj2.obj1 = obj1
let c = Container()
c.obj1 = obj1
c.obj2 = obj2
Run Code Online (Sandbox Code Playgroud)
控制台上的结果是:
Container Deinit
Run Code Online (Sandbox Code Playgroud)
有人可以指出为什么Container即使它的属性不被解除分配?
作为Cocoa/Obj-C的新手,我正在通过Aaron Hillegass的"Cocoa Programming for Mac OS X"一书,而且 - 现在我们还有机会使用GC来避免所有这些推理 - 我不是我确定我得到了一些保留的理由.
特别是在其中一个例子中,Aaron提供了良好的编程实践:
- (void) setFoo:(NSCalendarDate *)x
{
[x retain];
[foo release];
foo = x;
}
Run Code Online (Sandbox Code Playgroud)
我没有理由在方法的第一行保留x实例:
[x retain];
Run Code Online (Sandbox Code Playgroud)
这个实例的范围只是set方法,对吧?退出方法范围时,x实例应该被解除分配吗?此外,在将x分配给foo时:
foo = x;
Run Code Online (Sandbox Code Playgroud)
无论如何foo将指向x内存单元格,因此会增加指向对象的保留计数,不是吗?这应该确保不会释放内存.
那么,有什么意义呢?当然,我确信我遗失了一些东西,但不知道到底是什么.
谢谢,Fabrizio
retaincount ×10
objective-c ×7
cocoa ×3
iphone ×3
memory ×2
retain-cycle ×2
cocoa-touch ×1
dealloc ×1
ios5 ×1
memory-leaks ×1
rx-swift ×1
swift ×1
swift2 ×1
types ×1