• 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
Re: NSDocument/NSFileWrapper troubles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDocument/NSFileWrapper troubles


  • Subject: Re: NSDocument/NSFileWrapper troubles
  • From: Derek Chesterfield <email@hidden>
  • Date: Thu, 31 Jul 2003 09:27:25 +0100

I can't say that I have the answer, as I am a 'novice', but I have highlighted a couple of points in the code below:

On Thursday, July 31, 2003, at 8:58 AM, Christopher Erbach wrote:

I'm having troubles with some NSDocument code that I, a Cocoa newbie, wrote. I'm trying to preserve an NSFileWrapper passed by loadFileWrapperRepresentation:ofType: within the NSDocument. If I create a new file, I can save it, even multiple times, and all's well and good. However, when I load a file from disk and try to save it again, my application crashes.

If I comment out my "wrapper = newWrapper" line in loadFileWrapperRepresentation, the application runs and saves, albeit without loading any of the data. This looks eerily like a simple retain problem, but if it is, my newbie eye certainly can't find it.

I sliced the detailed pieces for loading and saving out of the routines. What's left is enough to cause the crashing behavior when saving files, which actually occurs after the loading/saving is complete during the event loop, not anywhere that's easily debuggable.

~ Chris Erbach



// From CocoaDream.h:

@interface CocoaDream : NSDocument
{
NSFileWrapper * wrapper;
IBOutlet NSTextView * entryBody;
IBOutlet NSFormCell * subjectBox;
IBOutlet NSFormCell * dateBox;
}

// From CocoaDream.m:

- (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)newWrapper ofType:(NSString *)type {
NSEnumerator * fileEnum;
id curFile;
NSMutableDictionary * metadata;
NSString ** error = nil;
I would remove all these unused variables. Especially that NSString **error [is ** even valid in ObjC? I've never seen a double-dereference in any sample code before]

[newWrapper retain];
[wrapper release]; // Useless, logically, conventionally, valuable
wrapper = newWrapper; // Commenting this line prevents the crashes

return true;
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];

[wrapper release];

if (wrapper == nil) {
// Only used if we haven't loaded a wrapper, earlier, from loadFileWrapperRepresentation yet
wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:[[NSDictionary alloc] init]];
Use [NSDictionary dictionary]. This will then be in the autorelease pool. At least, I think I am right: I don't think YOU should be retaining the dictionary, the the USER (NSFileWrapper) should be doing that.

Also, you are releasing the wrapper if you got one from loadFileWrapperRepresentation. but if you didn't you allocate one, but don't release it. ???

}
}

- (NSFileWrapper *)fileWrapperRepresentationOfType:(NSString *)type {
NSMutableDictionary * metaData = [[NSMutableDictionary alloc] init];
NSData * htmlData = nil;
NSString ** error = nil;
More unused variables can go. Especially the metaData, for which you create and (implicitly) retain a dictionary for, but which is never used, nor released. And there's another **.


return wrapper;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: NSDocument/NSFileWrapper troubles: solved
      • From: Christopher Erbach <email@hidden>
References: 
 >NSDocument/NSFileWrapper troubles (From: Christopher Erbach <email@hidden>)

  • Prev by Date: NSDocument/NSFileWrapper troubles
  • Next by Date: Re: NSDocument/NSFileWrapper troubles
  • Previous by thread: NSDocument/NSFileWrapper troubles
  • Next by thread: Re: NSDocument/NSFileWrapper troubles: solved
  • Index(es):
    • Date
    • Thread