help to resolve memory leak problem?

Discussion in 'Mac Programming' started by mkristain, Sep 27, 2011.

  1. mkristain macrumors regular

    Joined:
    Aug 18, 2011
    #1
    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.
     
  2. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    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?
     
  4. mkristain, Sep 28, 2011
    Last edited: Sep 28, 2011

    mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #5
    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.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    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
     
  6. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #7
    means after every 50 images delete all images from image browser.

    is there any other way.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    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
     
  8. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    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
     
  10. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #11
    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..
     
  11. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #12
    You still have at least one very obvious memory leak.
     
  12. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
  13. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #14
    If you can't find it yourself, you should re-read the memory management guide.
     
  14. GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #15
    This is a great article that may help you solve many of your problems.
     
  15. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #16
    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.
     
  16. Kenndac macrumors 6502

    Kenndac

    Joined:
    Jun 28, 2003
    #17
    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.
     
  17. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #18
    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.
     
  18. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #19
    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.
     
  19. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #20
    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.
     
  20. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
  21. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #22
    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.
     

Share This Page