Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mkristain

macrumors regular
Original poster
Aug 18, 2011
115
0
hi ,

i am display the image in IkImageBrowserView using IKImageBrowserNSDataRepresentationType, but here is memory leak please anybody can help me to find out this and resolve this.

Code:
@interface MyImageObject : NSObject 
{
	NSMutableData * imageData;
  	NSString * image_Name;
}
@end

@implementation MyImageObject


- (id)initWithImage_data:(NSMutableData *)animageData image_Name:(NSString *)anImageName
{
	if (self = [super init]) 
	{
		imageData = [animageData copy];
		image_Name = [anImageName copy];
	}
	return self;
}

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


/* let the image browser knows we use a path representation */
- (NSString *)  imageRepresentationType
{	
	return IKImageBrowserNSDataRepresentationType;
}

/* give our representation to the image browser */
- (id)  imageRepresentation
{
	return imageData;	
}

/* use the absolute filepath as identifier */
- (NSString *) imageUID
{
	return image_Name;
}

- (id) imageTitle
{
	return image_Name;
}


@end

and use this method from main class to pass NSMutabledata for image file

- (void) addImageWithData:(NSMutableData *)animageData imageName:(NSString*)anImageName
{   
	if([animageData length]==0)
	{
		return;
	}
	
NSString * noprevfile=@"";

	MyImageObject *item ;
	
    NSAutoreleasePool *rel_pool = [NSAutoreleasePool new];

	
	NSImage *thisImage = [[NSImage alloc]initWithData:animageData];
	
	if(![thisImage isValid])
	{
		noprevfile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"NoPreview.png"];

		if([fileManager fileExistsAtPath:noprevfile])
		{
			animageData=[[NSData alloc] initWithContentsOfFile:noprevfile];
		}
	}
		
	[thisImage release];
	 
	
	item = [[MyImageObject alloc] initWithImage_data:animageData image_Name:anImageName]  ;

	[images addObject:item];
	
	[animageData setLength:0];
	
	[item release];
	
	[rel_pool release];
	
	[imageBrowser reloadData];
	
	[imageBrowser scrollIndexToVisible:[images count] - 1];
	
}

please help me.

thanks.
 
No, you don't.

It would also be extremely helpful if you actually told us what specifically is leaking. Surely you used Instruments to investigate this first, right?
 
when i add image from a directory that have contains approx 2000 images, so call addImageWithData method to display the image than memory problem is occur, i really unable to find out.

malloc: *** mmap(size=16371712) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
please help me how to find this using Instruments.
 
Last edited:
when i add image from a directory that have contains approx 2000 images, so call addImageWithData method to display the image than memory problem is occur, i really unable to find out.


please help me how to find this using Instruments.

Are all 2000 images requiring 16MB? If so, 32GB of RAM is pretty egregious. You might need a new strategy. Maybe 50 at a time?

-Lee
 
means after every 50 images delete all images from image browser.

is there any other way.
 
Your handling of invalid images is not good. There are a few leaks in there. Also, why keep many copies? Lazy load a single copy (if null, read once, use. If not null, use current object). I'm used to seeing a pool drain, not just a release. Also, why not store NSImages instead of nsdata? You're doing the conversion already, only to check its validity. Why not use it? Also, what code is pulling the NSData from disk? Why not just straight into NSImage? Also, why copy the elements in MyImageObject? Why not just retain?

-Lee
 
No, I'm posting from a phone. Basically, you have a member:
NSImage *invalidImage = nil;

Then,
if(invalid) {
if(invalidImage == nil) {
invalidImage = ...;
}
imageToUse = invalidImage;
}

In terms of just using NSImage you just return
IKImageBrowserNSImageRepresentationType
from
imageRepresentationType

Then store and return an NSImage. In initWithImage_..., just assign the pointers passed in, assign to your members, and retain.

Not sure where your reading all your images and calls addImageWithData? I'd change that to accept an NSImage, read stuff up as an NSImage, and pass it to that method. Now each object is read once and new objects aren't being constantly created/copied,etc.

-Lee
 
now i use this but the problem is not solve..

Code:
- (void) addImageWithData:(NSMutableData *)animageData imageName:(NSString*)anImageName
{   
	if([animageData length]==0)
	{
		return;
	}
	
	
    NSAutoreleasePool *rel_pool = [NSAutoreleasePool new];

	MyImageObject *item ;
	
	NSImage *thisImage = [[NSImage alloc]initWithData:animageData];
	
	if(![thisImage isValid])
	{
		noprevfile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @"NoPreview.png"];

		if([fileManager fileExistsAtPath:noprevfile])
		{
			animageData=[[NSData alloc] initWithContentsOfFile:noprevfile];
			
			thisImage = [[NSImage alloc]initWithData:animageData];
			
			item = [[MyImageObject alloc] initWithImage:thisImage image_Name:anImageName];

		}
	}
	else
	{
		item = [[MyImageObject alloc] initWithImage:thisImage image_Name:anImageName];
		
	}

	[thisImage release];

	[images addObject:item];
	
	[item release];
	
	[rel_pool drain];
	
	[imageBrowser reloadData];
	
	[imageBrowser scrollIndexToVisible:[images count] - 1];
	
}

and 

/* let the image browser knows we use a path representation */
- (NSString *)  imageRepresentationType
{
	return IKImageBrowserNSImageRepresentationType;
}

/* give our representation to the image browser */
- (id)  imageRepresentation
{
	return image;
}


- (id)initWithImage:(NSImage*)anImage image_Name:(NSString*)anImageName
{
	if (self = [super init]) 
	{	
		image = [anImage copy];
		
		if(image_Name != anImageName){
			[image_Name release];
			image_Name = [anImageName retain];
		}
	}
	return self;
}

please help me..
 
This is a great article that may help you solve many of your problems.

Thank you for the article GorillaPaws. It's helped me to identify that I'm feeling exhausted, bitter and dispirited; and why. I've not even been willing to help the likes of Lars much anymore. Perhaps now that I'm aware of it, I can recharge, defend against it, help more in the future.
 
Also, copying in the data into RAM for every single image is a super bad idea. If they're on disk, you can have virtually unlimited images in an IKImageBrowserView using IKImageBrowserPathRepresentationType - the image browser itself will then load and unload the images as needed.
 
Thank you for the article GorillaPaws. It's helped me to identify that I'm feeling exhausted, bitter and dispirited; and why. I've not even been willing to help the likes of Lars much anymore. Perhaps now that I'm aware of it, I can recharge, defend against it, help more in the future.

At least I'm pretty sure Lars is just trying to learn. I'm fully expecting to find mkristain's app on the App Store as soon as we're done writing it for him.
 
Are all 2000 images requiring 16MB? If so, 32GB of RAM is pretty egregious. You might need a new strategy. Maybe 50 at a time?

-Lee

now i [images removeAllObjects]; after every 50 but still the memory not release.

please tell me if i remove all object than how can i release memory that this contain by IKImageBrowserView.

thanks.
 
now i [images removeAllObjects]; after every 50 but still the memory not release.

please tell me if i remove all object than how can i release memory that this contain by IKImageBrowserView.

thanks.

Gorillapaws posted a great link to an article that will identify many of your problems. You should really go to that link, read it carefully, and take it to heart. Your future is at stake.
 
thanks for all ur help.

So, mkristain, what would you say about that article about "Help Vampires" that Gorillapaws linked to? If you look at yourself, do you think you are a "Help Vampire" who just drains a helpful community without returning anything? What could you say to convince people that you are not?

A hint: "thanks for all ur help." is not it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.