0

我在 Xcode 中使用 iPhone 6 模拟器。在我的视图中添加一个简单的 UITextView 后,我在其中输入了一些单词。但是我在控制台中发现了一些错误:

Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSaveGState: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextDrawLinearGradient: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSaveGState: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextDrawLinearGradient: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextSetFillColorWithColor: invalid context 0x0
Oct 19 10:07:14 localhost textViewTest[1438] <Error>: CGContextFillRects: invalid context 0x0

而且当我看到内存使用时,它增加得非常快。但是当我使用iphone5或iphone4.3模拟器时它不会出现。

这是我的代码:

#import "ViewController.h"
#include <sys/sysctl.h>
#include <mach/mach.h>
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(0,0,320,50)];
    [self.view addSubview:textView];
    [textView release];

    NSTimeInterval timeInterval =5.0 ;
    [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                 target:self
                               selector:@selector(handleMaxShowTimer:)
                               userInfo:nil
                                repeats:YES];

}
- (double)availableMemory
{
    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(),HOST_VM_INFO,(host_info_t)&vmStats,&infoCount);
    if(kernReturn != KERN_SUCCESS)
    {
        return NSNotFound;
    }
    return ((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}

- (double)usedMemory
{
    task_basic_info_data_t taskInfo;
    mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
    kern_return_t kernReturn = task_info(mach_task_self(),
                                     TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
    if(kernReturn != KERN_SUCCESS) {
        return NSNotFound;
    }
    return taskInfo.resident_size / 1024.0 / 1024.0;
}

-(void)handleMaxShowTimer:(NSTimer *)theTimer
{
    NSLog(@" use memory  %f  remain memory  %f",[self usedMemory],[self availableMemory]);

}
@end
4

1 回答 1

0

您的内存使用量很可能不是“疯狂地增加”,您只是碰巧正在查看仪器中分配的总字节数,这意味着稳步增加。使用实际的泄漏工具,它甚至会告诉您发生泄漏的确切代码行,以及某个特定功能对整体泄漏的贡献程度。

至于 CG... 错误,需要在-drawRect:覆盖中完成绘制代码,这是唯一可以获得稳定的非 NULL 上下文进行绘制的地方。

于 2012-10-19T02:47:10.393 回答