Skip to content

Commit

Permalink
Fixing thread safety issue and potential memory leak in DDLogMessage.…
Browse files Browse the repository at this point in the history
… Fixes issue #21
  • Loading branch information
robbiehanson committed Mar 28, 2012
1 parent fbff870 commit 9f0e677
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
14 changes: 3 additions & 11 deletions Lumberjack/DDLog.h
Expand Up @@ -425,14 +425,6 @@ NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
char *queueLabel;
NSString *threadName;
id tag; // For 3rd party extensions to the framework, where flags and contexts aren't enough.

// The private variables below are only calculated if needed.
// You should use the public methods to access this information.

@private
NSString *threadID;
NSString *fileName;
NSString *methodName;
}

/**
Expand All @@ -457,18 +449,18 @@ NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
* Returns the threadID as it appears in NSLog.
* That is, it is a hexadecimal value which is calculated from the machThreadID.
**/
@property (nonatomic, readonly) NSString *threadID;
- (NSString *)threadID;

/**
* Convenience property to get just the file name, as the file variable is generally the full file path.
* This method does not include the file extension, which is generally unwanted for logging purposes.
**/
@property (nonatomic, readonly) NSString *fileName;
- (NSString *)fileName;

/**
* Returns the function variable in NSString form.
**/
@property (nonatomic, readonly) NSString *methodName;
- (NSString *)methodName;

@end

Expand Down
24 changes: 6 additions & 18 deletions Lumberjack/DDLog.m
Expand Up @@ -806,32 +806,20 @@ - (id)initWithLogMsg:(NSString *)msg

- (NSString *)threadID
{
if (threadID == nil)
{
threadID = [[NSString alloc] initWithFormat:@"%x", machThreadID];
}

return threadID;
return [[NSString alloc] initWithFormat:@"%x", machThreadID];
}

- (NSString *)fileName
{
if (fileName == nil && file != NULL)
{
fileName = DDExtractFileNameWithoutExtension(file, NO);
}

return fileName;
return DDExtractFileNameWithoutExtension(file, NO);
}

- (NSString *)methodName
{
if (methodName == nil && function != NULL)
{
methodName = [[NSString alloc] initWithUTF8String:function];
}

return methodName;
if (function == NULL)
return nil;
else
return [[NSString alloc] initWithUTF8String:function];
}

- (void)dealloc
Expand Down

0 comments on commit 9f0e677

Please sign in to comment.