开发者

NSMutableArray EXC_BAD_EXCESS SIGBUS

开发者 https://www.devze.com 2022-12-26 10:03 出处:网络
I\'ve the following code which causes crashes after sometime as i\'ve set the below code in a timer: CGImageRef cgImage = U开发者_Python百科IGetScreenImage();

I've the following code which causes crashes after sometime as i've set the below code in a timer:

CGImageRef cgImage = U开发者_Python百科IGetScreenImage();
[array addObject:(id)cgImage];
CGImageRelease(cgImage);

Where initiallly i've declared array as:

array = [[NSMutableArray alloc] init];

The timer goes well till 10 seconds as timer is of 1/10 seconds after 10 seconds it crashes.

I think the application crashes because of EXC_BAD_EXCESS but dont know how to solve. Can anybody help in solving the problem?

Thanks in Adv.


addObject: will raise an exception if the object is nil. Try this:

array = [[NSMutableArray alloc] initWithCapacity:1]; //designated initializer
CGImageRef cgImage = UIGetScreenImage();
if(cgImage)
{
    [array addObject:cgImage];
    CGImageRelease(cgImage);
}


From the Apple Developer Forum to UIGetScreenImage():

As you use this function, please note that it returns a retained CGImageRef and manage your memory accordingly.


Are you sure you're supposed to be releasing cgImage?

I don't see documentation for UIGetScreenImage(), but if it follows the Create Rule, I would not expect you to need to release the object (because the function does not have "Create" or "Copy" in its name).

EDIT: I've since found several references that say that you do need to release the image, despite the name of the function. (Which has apparently been renamed UICreateScreenImage() in the 3.2 SDK.

0

精彩评论

暂无评论...
验证码 换一张
取 消