Skip to content

Commit

Permalink
renamed to satisfy the create rule for memory management: getShortLiv…
Browse files Browse the repository at this point in the history
…edStringFromData -> newShortLivedStringFromData and GetRandomizedFileName -> CreateRandomizedFileName; add URLs to notes imported from TSV files; use date modified when adding files from disk as notes; added modifiedDateOfNote() note-method; avoid a crash when a note's underlying UTF8 content cache string could be created; fix many potential memory leaks
  • Loading branch information
Zachary Schneirov committed Sep 29, 2009
1 parent ca807ca commit 4216a4e
Show file tree
Hide file tree
Showing 17 changed files with 1,285 additions and 473 deletions.
20 changes: 10 additions & 10 deletions AlienNoteImporter.m
Expand Up @@ -197,7 +197,7 @@ - (void)URLGetter:(URLGetter*)getter returnedDownloadedFile:(NSString*)filename
NSArray *notes = [self notesInFile:filename];
if ([notes count]) {

NSMutableAttributedString *content = [[[notes lastObject] contentString] mutableCopy];
NSMutableAttributedString *content = [[[[notes lastObject] contentString] mutableCopy] autorelease];
if ([[[content string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) {
//only add string if it has at least one non-whitespace character
[content prefixWithSourceString:[[getter url] absoluteString]];
Expand All @@ -223,6 +223,7 @@ - (void)URLGetter:(URLGetter*)getter returnedDownloadedFile:(NSString*)filename
uniqueFilename:nil format:SingleDatabaseFormat];

[receptionDelegate noteImporter:self importedNotes:[NSArray arrayWithObject:noteObject]];
[noteObject autorelease];
}
}

Expand Down Expand Up @@ -321,7 +322,7 @@ - (NoteObject*)noteWithFile:(NSString*)filename {
//try PDFKit if on 10.4
if (RunningTigerAppKitOrHigher) {
Class PdfDocClass = [[self class] PDFDocClass];
if (PdfDocClass != Nil) {
if (PdfDocClass != Nil) {
id doc = [[PdfDocClass alloc] initWithData:[NSData dataWithContentsOfFile:filename]];
if (doc) {
id sel = [doc performSelector:@selector(selectionForEntireDocument)];
Expand All @@ -343,7 +344,7 @@ - (NoteObject*)noteWithFile:(NSString*)filename {

NSMutableString *stringFromData = nil;
NSStringEncoding defaultEncoding = [NSString defaultCStringEncoding];
if ((stringFromData = [NSMutableString getShortLivedStringFromData:[NSMutableData dataWithContentsOfFile:filename] ofGuessedEncoding:&defaultEncoding])) {
if ((stringFromData = [NSMutableString newShortLivedStringFromData:[NSMutableData dataWithContentsOfFile:filename] ofGuessedEncoding:&defaultEncoding])) {
attributedStringFromData = [[NSMutableAttributedString alloc] initWithString:stringFromData
attributes:[[GlobalPrefs defaultPrefs] noteBodyAttributes]];
[stringFromData release];
Expand All @@ -359,20 +360,18 @@ - (NoteObject*)noteWithFile:(NSString*)filename {
[attributedStringFromData trimLeadingWhitespace];
[attributedStringFromData removeAttachments];
[attributedStringFromData santizeForeignStylesForImporting];
[attributedStringFromData autorelease];

//we do not use filename as uniqueFilename, as we are only importing--not taking ownership
NoteObject *noteObject = [[NoteObject alloc] initWithNoteBody:attributedStringFromData title:[[filename lastPathComponent] stringByDeletingPathExtension]
uniqueFilename:nil format:SingleDatabaseFormat];
if (noteObject) {
//[noteObject setDateAdded:CFDateGetAbsoluteTime((CFDateRef)[attributes objectForKey:NSFileCreationDate])];
//[noteObject setDateModified:CFDateGetAbsoluteTime((CFDateRef)[attributes objectForKey:NSFileModificationDate])];
//TODO: take date/created modified; why throw away information?

[attributedStringFromData release];
[noteObject setDateModified:CFDateGetAbsoluteTime((CFDateRef)[attributes objectForKey:NSFileModificationDate])];

return [noteObject autorelease];
} else {
NSLog(@"couldn't generate note object from sticky note??");
NSLog(@"couldn't generate note object from imported attributed string??");
}

}
Expand Down Expand Up @@ -529,7 +528,7 @@ - (NSArray*)_importTSVFile:(NSString*)filename
{
NSMutableString *contents = nil;
NSStringEncoding defaultEncoding = [NSString defaultCStringEncoding];
if (!(contents = [NSMutableString getShortLivedStringFromData:[NSMutableData dataWithContentsOfFile:filename] ofGuessedEncoding:&defaultEncoding])) {
if (!(contents = [NSMutableString newShortLivedStringFromData:[NSMutableData dataWithContentsOfFile:filename] ofGuessedEncoding:&defaultEncoding])) {
return nil;
}

Expand Down Expand Up @@ -558,7 +557,8 @@ - (NSArray*)_importTSVFile:(NSString*)filename
continue;

NSString *title = [fields objectAtIndex:0];
NSAttributedString *attributedBody = [[[NSMutableAttributedString alloc] initWithString:s attributes:[[GlobalPrefs defaultPrefs] noteBodyAttributes]] autorelease];
NSMutableAttributedString *attributedBody = [[[NSMutableAttributedString alloc] initWithString:s attributes:[[GlobalPrefs defaultPrefs] noteBodyAttributes]] autorelease];
[attributedBody addLinkAttributesForRange:NSMakeRange(0, [attributedBody length])];

NoteObject *note = [[[NoteObject alloc] initWithNoteBody:attributedBody title:title uniqueFilename:nil format:SingleDatabaseFormat] autorelease];
if (note) {
Expand Down
4 changes: 1 addition & 3 deletions AppController.m
Expand Up @@ -30,8 +30,6 @@ @implementation AppController
- (id)init {
if ([super init]) {

NSLog(@"gcc version: %d", GCC_VERSION);

windowUndoManager = [[NSUndoManager alloc] init];

isCreatingANote = isFilteringFromTyping = typedStringIsCached = NO;
Expand Down Expand Up @@ -363,6 +361,7 @@ - (BOOL)addNotesFromPasteboard:(NSPasteboard*)pasteboard {
if (pboardString) newString = [[NSMutableAttributedString alloc] initWithString:pboardString];
}

[newString autorelease];
if ([newString length] > 0) {
[newString removeAttachments];

Expand All @@ -373,7 +372,6 @@ - (BOOL)addNotesFromPasteboard:(NSPasteboard*)pasteboard {
}
[newString santizeForeignStylesForImporting];
note = [notationController addNote:newString withTitle:noteTitle];
[newString release];
return note != nil;
}

Expand Down
3 changes: 1 addition & 2 deletions AttributedPlainText.m
Expand Up @@ -14,8 +14,7 @@
@implementation NSMutableAttributedString (AttributedPlainText)

- (void)trimLeadingWhitespace {
NSMutableCharacterSet *whiteSet = [[NSMutableCharacterSet alloc] init];
[whiteSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableCharacterSet *whiteSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
//include attachment characters and non-breaking spaces. anything else?
unichar badChars[2] = { NSAttachmentCharacter, 0x00A0 };
[whiteSet addCharactersInString:[NSString stringWithCharacters:badChars length:2]];
Expand Down
2 changes: 1 addition & 1 deletion BufferUtils.c
Expand Up @@ -162,7 +162,7 @@ CFStringRef GetRelativeDateStringFromTimeAndLocaleInfo(CFAbsoluteTime time, CFSt
}
#endif

CFStringRef GetRandomizedFileName() {
CFStringRef CreateRandomizedFileName() {
static int sequence = 0;

sequence++;
Expand Down
2 changes: 1 addition & 1 deletion BufferUtils.h
Expand Up @@ -19,7 +19,7 @@ unsigned DumbWordCount(const void *s1, size_t len);
int genericSortContextFirst(int (*context) (void*, void*), void* one, void* two);
int genericSortContextLast(void* one, void* two, int (*context) (void*, void*));
void QuickSortBuffer(void **buffer, unsigned int objCount, int (*compar)(const void *, const void *));
CFStringRef GetRandomizedFileName();
CFStringRef CreateRandomizedFileName();
OSStatus FSCreateFileIfNotPresentInDirectory(FSRef *directoryRef, FSRef *childRef, CFStringRef filename, Boolean *created);
OSStatus FSRefMakeInDirectoryWithString(FSRef *directoryRef, FSRef *childRef, CFStringRef filename, UniChar* charsBuffer);
OSStatus FSRefReadData(FSRef *fsRef, size_t maximumReadSize, UInt64 *bufferSize, void** newBuffer, UInt16 modeOptions);
Expand Down
3 changes: 2 additions & 1 deletion ExporterManager.m
Expand Up @@ -67,6 +67,7 @@ - (void)exportPanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contex

FSRef directoryRef;
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)directory, kCFURLPOSIXPathStyle, true);
[(id)url autorelease];
if (!url || !CFURLGetFSRef(url, &directoryRef)) {
NSRunAlertPanel([NSString stringWithFormat:NSLocalizedString(@"The notes couldn't be exported because the directory quotemark%@quotemark couldn't be accessed.",nil),
[directory stringByAbbreviatingWithTildeInPath]], @"", NSLocalizedString(@"OK",nil), nil, nil);
Expand Down Expand Up @@ -98,7 +99,7 @@ - (void)exportPanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contex
NSString *exportErrorTitleString = [NSString stringWithFormat:NSLocalizedString(@"The note quotemark%@quotemark couldn't be exported because %@.",nil),
titleOfNote(note), [NSString reasonStringFromCarbonFSError:err]];
if (!lastNote) {
result = NSRunAlertPanel(exportErrorTitleString, @"", NSLocalizedString(@"OK",nil), nil, nil, nil);
NSRunAlertPanel(exportErrorTitleString, @"", NSLocalizedString(@"OK",nil), nil, nil, nil);
} else {
result = NSRunAlertPanel(exportErrorTitleString, NSLocalizedString(@"Continue exporting?", @"alert title for exporter interruption"),
NSLocalizedString(@"Continue", @"(exporting notes?)"), NSLocalizedString(@"Stop Exporting", @"(notes?)"), nil);
Expand Down
3 changes: 2 additions & 1 deletion LinkingEditor.m
Expand Up @@ -622,10 +622,11 @@ - (NSRange)highlightTermsTemporarilyReturningFirstRange:(NSString*)typedString {
NSLog(@"highlightTermsTemporarily: Invalid range (%@)", range ? NSStringFromRange(*(NSRange*)range) : @"?");
}
}
CFRelease(ranges);
}
}
CFRelease(terms);
}

return (firstRange);
}

Expand Down
1 change: 1 addition & 0 deletions NSCollection_utils.m
Expand Up @@ -161,6 +161,7 @@ - (void)addMenuItemsForURLsInNotes:(NSMenu*)urlsMenu {
[item setRepresentedObject:[url absoluteString]];
[item setTarget:[item representedObject]];
[urlsMenu addItem:item];
[item release];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion NSString_NV.h
Expand Up @@ -46,7 +46,7 @@ void resetCurrentDayTime();
@end

@interface NSMutableString (NV)
+ (NSMutableString*)getShortLivedStringFromData:(NSMutableData*)data ofGuessedEncoding:(NSStringEncoding*)encoding;
+ (NSMutableString*)newShortLivedStringFromData:(NSMutableData*)data ofGuessedEncoding:(NSStringEncoding*)encoding;
@end

@interface NSEvent (NV)
Expand Down
11 changes: 5 additions & 6 deletions NSString_NV.m
Expand Up @@ -180,7 +180,7 @@ - (CFArrayRef)copyRangesOfWordsInString:(NSString*)findString inRange:(NSRange)l
}
//should sort them all now by location
//CFArraySortValues(allRanges, CFRangeMake(0, CFArrayGetCount(allRanges)), <#CFComparatorFunction comparator#>,<#void * context#>);

CFRelease(terms);
return allRanges;
}

Expand Down Expand Up @@ -213,7 +213,7 @@ - (NSMutableString*)stringByReplacingOccurrencesOfString:(NSString*)stringToRepl

+ (NSString*)timeDelayStringWithNumberOfSeconds:(double)seconds {
unichar ch = 0x2245;
NSString *approxCharStr = nil;
static NSString *approxCharStr = nil;
if (!approxCharStr) approxCharStr = [[NSString stringWithCharacters:&ch length:1] retain];
if (seconds < 1.0) {
return [NSString stringWithFormat:@"%@ %0.0f ms", approxCharStr, seconds*1000];
Expand Down Expand Up @@ -343,7 +343,7 @@ - (NSAttributedString*)attributedPreviewFromBodyText:(NSAttributedString*)bodyTe

[bodySummary release];

return attributedStringPreview;
return [attributedStringPreview autorelease];
}

//the following three methods + function come courtesy of Mike Ferris' TextExtras
Expand Down Expand Up @@ -403,7 +403,7 @@ - (unsigned)numberOfLeadingSpacesFromRange:(NSRange*)range tabWidth:(unsigned)ta
unsigned tabW = tabWidth;
NSUInteger endOfWhiteSpaceIndex = NSNotFound;

if (range->length == 0) {
if (!range || range->length == 0) {
return 0;
}

Expand Down Expand Up @@ -471,7 +471,6 @@ - (const char*)lowercaseUTF8String {
const char *utf8String = [(NSString*)str2 UTF8String];

CFRelease(str2);

return utf8String;
}

Expand Down Expand Up @@ -566,7 +565,7 @@ - (NSTextView*)getTextViewWithFrame:(NSRect*)theFrame {

@implementation NSMutableString (NV)

+ (NSMutableString*)getShortLivedStringFromData:(NSMutableData*)data ofGuessedEncoding:(NSStringEncoding*)encoding {
+ (NSMutableString*)newShortLivedStringFromData:(NSMutableData*)data ofGuessedEncoding:(NSStringEncoding*)encoding {

//this will fail if data lacks a BOM
NSMutableString* stringFromData = [data newStringUsingBOMReturningEncoding:encoding];
Expand Down

0 comments on commit 4216a4e

Please sign in to comment.