Re: Importing Word doc in Carbon app via Cocoa
Re: Importing Word doc in Carbon app via Cocoa
- Subject: Re: Importing Word doc in Carbon app via Cocoa
- From: Shawn Erickson <email@hidden>
- Date: Thu, 16 Apr 2009 08:51:57 -0700
On Thu, Apr 16, 2009 at 8:32 AM, Steve Mills <email@hidden> wrote:
> Any ideas on what might be wrong? I appreciate any help.
Simple misapplication of Cocoa memory management. Read the following
(seriously it will save you a lot of time)...
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html>
> // Convert the Word doc data into an attributed string:
> nsdata = [NSData dataWithBytes:data length:len];
The object pointed to by nsdata was made available to you by a method
that doesn't also give you an "ownership role" for that object.
> if(nsdata != nil) {
> attrStr = [[NSAttributedString alloc]
> initWithDocFormat:nsdata documentAttributes:&attr];
> [nsdata release];
You then proceed to release the object pointed to by nsdata but you
never retained it before this point in time (aka didn't take an
ownership role so no need to give up ownership).
> if(attrStr != nil) {
> // Create rtf data from the
> attributed string:
> NSRange range =
> NSMakeRange(0, [[attrStr string] length]);
>
> nsdata = [attrStr RTFFromRange:range
> documentAttributes:attr];
Again the object pointed to by nsdata was made available to you by a
method that doesn't also give you an "ownership role" for that object.
> [attrStr release];
>
> if(nsdata != nil) {
> outData.append([nsdata
> length], (char*)[nsdata bytes]);
> [nsdata release];
Again you then proceed to release the object pointed to by nsdata but
you never retained it before this point in time (aka didn't take an
ownership role so no need to give up ownership).
> }
> }
> }
>
> [localPool release];
Then things crash under the above call (or at the end of the next
event loop) because you over released objects out from under the auto
release pool who was asked to release those same objects by the
methods that originally gave your code access to the NSData instances.
-Shawn
_______________________________________________
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