Objective C内存管理问题

发布于 2024-12-08 10:35:21 字数 2774 浏览 3 评论 0原文

我在使用一些目标 C 代码在 IKImageBrowserView 上加载图像时遇到了这个问题。 我正在遵循苹果的图像浏览器示例,但我仍然在某些时候失败,我猜测它的内存管理。 这是一些代码:

/* Our datasource object */
@interface myImageObject : NSObject
{
    NSString *_path; 
}
@end
@implementation myImageObject

- (void)dealloc
{
    [_path release];
    [super dealloc];
}

/* our datasource object is just a filepath representation */
- (void)setPath:(NSString *)path
{
    NSLog(@"setting path for %@",path);
    if(_path != path)
    {
     [_path release];
        _path = [path retain];
    }
}

现在,看来我正确地保留了对象内的路径值。 现在来看控制器代码:

-(IBAction)loadMosaicImages:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *urls = [openFiles() retain];
NSInteger n;
n = [urls count];

NSURL *url = [urls objectAtIndex:0];
[self parsePathForImages:[url path]];
[urls release];
[pool drain];
}
- (void)updateDatasource
{
    NSLog(@" UDS-> _importedImages length  : %@",[_importedImages count]);
    //-- update our datasource, add recently imported items    
    [_images addObjectsFromArray:_importedImages];

    //-- empty our temporary array
    [_importedImages removeAllObjects];
    NSLog(@" UDS-> _images length  : %@",[_images count]);
    //-- reload the image browser and set needs display
    [_imageBrowser reloadData];
}
-(void)parsePathForImages:(NSString *)path{

    NSLog(@"Directory within thread method is %@",path);
    NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    myImageObject *p;
    for(int i=0;i<content.count;i++)
    {
       // NSLog(@"%@",(NSString *)[content objectAtIndex:i]);
        NSLog(@"Complete : %@",[path stringByAppendingPathComponent:(NSString *)[content objectAtIndex:i]]);
        /* add a path to our temporary array */
        p = [[myImageObject alloc] init];
        [p setPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
        [_importedImages addObject:p];
        [p release];
    }
    [self updateDatasource];
}

这就是所有相关代码。 _images 和 _importedImages 是在 awake from nib 方法中分配和初始化的 NSMutableArrays,而 openFiles() 是一个静态方法,用于打开 NSOpenpanel 并返回路径的 NSArray。

其调试输出执行以下操作:

Directory within thread method is /Users/cromestant/Code/images/
Complete : /Users/cromestant/Code/images/1.jpg
setting path for /Users/cromestant/Code/images/1.jpg
Complete : /Users/cromestant/Code/images/2.jpg
setting path for /Users/cromestant/Code/images/2.jpg
Complete : /Users/cromestant/Code/images/3.jpg
setting path for /Users/cromestant/Code/images/3.jpg

。 。 。 然后在 NSLog 上的 updateDataSource 方法的第一行停止崩溃,并带有“EXEC_BAD_ACCESS”,那么我的内存管理哪里出了问题? 我似乎正在创建一个 autoreleasePool,所以我有时间保留在其他地方,我释放我的对象..我真的不知道问题可能出在哪里。 提前致谢。

I'm having this issue with some objective C code to load the images on a IKImageBrowserView.
I'm following the image browser example from apple, but I still fail at some point and Im guessing its memory management.
Here is some of the code:

/* Our datasource object */
@interface myImageObject : NSObject
{
    NSString *_path; 
}
@end
@implementation myImageObject

- (void)dealloc
{
    [_path release];
    [super dealloc];
}

/* our datasource object is just a filepath representation */
- (void)setPath:(NSString *)path
{
    NSLog(@"setting path for %@",path);
    if(_path != path)
    {
     [_path release];
        _path = [path retain];
    }
}

Now, It seems I'm properly retaining the path value within the object.
now on to the controller code:

-(IBAction)loadMosaicImages:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *urls = [openFiles() retain];
NSInteger n;
n = [urls count];

NSURL *url = [urls objectAtIndex:0];
[self parsePathForImages:[url path]];
[urls release];
[pool drain];
}
- (void)updateDatasource
{
    NSLog(@" UDS-> _importedImages length  : %@",[_importedImages count]);
    //-- update our datasource, add recently imported items    
    [_images addObjectsFromArray:_importedImages];

    //-- empty our temporary array
    [_importedImages removeAllObjects];
    NSLog(@" UDS-> _images length  : %@",[_images count]);
    //-- reload the image browser and set needs display
    [_imageBrowser reloadData];
}
-(void)parsePathForImages:(NSString *)path{

    NSLog(@"Directory within thread method is %@",path);
    NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    myImageObject *p;
    for(int i=0;i<content.count;i++)
    {
       // NSLog(@"%@",(NSString *)[content objectAtIndex:i]);
        NSLog(@"Complete : %@",[path stringByAppendingPathComponent:(NSString *)[content objectAtIndex:i]]);
        /* add a path to our temporary array */
        p = [[myImageObject alloc] init];
        [p setPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
        [_importedImages addObject:p];
        [p release];
    }
    [self updateDatasource];
}

and that's all the relevant code. _images and _importedImages are NSMutableArrays alloced and inited in the awake from nib method, and openFiles() is a static method that opens an NSOpenpanel and returns an NSArray of the paths.

the debug output for this does this:

Directory within thread method is /Users/cromestant/Code/images/
Complete : /Users/cromestant/Code/images/1.jpg
setting path for /Users/cromestant/Code/images/1.jpg
Complete : /Users/cromestant/Code/images/2.jpg
setting path for /Users/cromestant/Code/images/2.jpg
Complete : /Users/cromestant/Code/images/3.jpg
setting path for /Users/cromestant/Code/images/3.jpg

.
.
.
then stops crashes at the first line of the method updateDataSource, on the NSLog with an 'EXEC_BAD_ACCESS'so where am I going wrong with the memory management?
I seem to be creating an autoreleasePool so ht I have time to retain somewhere else, I release my objects.. I really don't have a clue where the problem could be.
thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒七 2024-12-15 10:35:21

实际上,我认为你的问题可能不是内存管理。可能在这里:

NSLog(@" UDS-> _importedImages length  : %@",[_importedImages count]);

应该是

NSLog(@" UDS-> _importedImages length  : %d",[_importedImages count]);

因为 count 方法返回的是一个整数,而不是一个对象。

Actually, I think your problem might not be memory management. It might be here:

NSLog(@" UDS-> _importedImages length  : %@",[_importedImages count]);

It should be

NSLog(@" UDS-> _importedImages length  : %d",[_importedImages count]);

because the count method returns an integer, not an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文