• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
[NSImage initWithData]: Memory leak. Bug in code or in Framework?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[NSImage initWithData]: Memory leak. Bug in code or in Framework?


  • Subject: [NSImage initWithData]: Memory leak. Bug in code or in Framework?
  • From: Stéphane <email@hidden>
  • Date: Thu, 13 Dec 2007 21:04:45 +0100

This simple sample code results in a memory leak in [NSImage initWithData:].

Is it a bug in Cocoa or is it a bug in the code itself?

Is there a workaround (I already tried multiple things: mutableCopy of data to releaseit after initialization, setDataRetained and different cache mode for the NSImage).

No useful pointers found in the list archive or on the net.



Note that without the distribution notification communication, it works fine (this is to illustrate a problem seen in a tool receiving dist notifications).

-----------------------------------------------------

#import "ILeakThereforeIam.h"

@implementation ILeakThereforeIam

- (void) awakeFromNib
{
retainedImageData_=[[[NSImage imageNamed:@"NSApplicationIcon"] TIFFRepresentation] retain];

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationDidReceive:) name:@"DATA" object:nil suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];

[[NSTimer scheduledTimerWithTimeInterval:0.5f
target:self
selector:@selector(periodicTask:)
userInfo:nil
repeats:YES] retain];
}


- (void) notificationDidReceive:(NSNotification *) inNotification
{
	NSDictionary * tUserInfo;

	NSLog(@"receive notification");

	tUserInfo=[inNotification userInfo];

	if (tUserInfo!=nil)
	{
		NSData * tData;

		tData=[tUserInfo objectForKey:@"DATA"];

		if (tData!=nil)
		{
			NSImage * tImage;

			NSLog(@"Do you leak it?");

			tImage=[[NSImage alloc] initWithData:tData];

			if (tImage!=nil)
			{
				NSLog(@"Yes I do, see Activity Monitor");

				[tImage release];
			}
		}
	}
}

- (void) periodicTask:(NSTimer *) inTimer
{
NSDistributedNotificationCenter * tNotificationCenter;

tNotificationCenter=[NSDistributedNotificationCenter defaultCenter];

if (tNotificationCenter!=nil)
{
NSData * tData;

[tNotificationCenter postNotificationName:@"DATA" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:retainedImageData_,@"DATA",nil] deliverImmediately:YES];
}
}


@end


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • [Worse] Re: [NSImage initWithData]: Memory leak. Bug in code or in Framework?
      • From: Stephane Sudre <email@hidden>
  • Prev by Date: Re: How to read raw system disk
  • Next by Date: Re: NSBrowser with checkboxes?
  • Previous by thread: Re: How to read raw system disk
  • Next by thread: [Worse] Re: [NSImage initWithData]: Memory leak. Bug in code or in Framework?
  • Index(es):
    • Date
    • Thread