Prefix header with better releasing, freeing and logging


/ Published in: Objective C
Save to your folder(s)

This prefix header adds `SXRelease()`, `SXFree()` and `SXLog()` which can be used instead of `-[ release]`, `free()` and `NSLog()`. In order to use `SXLog()`, you must have DEBUG defined somewhere, such as in the build settings of the debug configuration.

Please note that the `while(0){}` is to prevent errors where a single `;` is not permitted. The optimizer will remove this from the assembly code.


Copy this code and paste it in your HTML
  1. #ifdef __OBJC__
  2.  
  3. #import <CoreFoundation/CoreFoundation.h>
  4. #import <Foundation/Foundation.h>
  5. #import <AppKit/AppKit.h>
  6. #import <Cocoa/Cocoa.h>
  7.  
  8. #define SXRelease(o) [o release]; o = nil
  9.  
  10. #ifdef DEBUG
  11. #define SXLog(...) NSLog(@"%@ > %s (line %d): %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:__VA_ARGS__, nil])
  12. #else
  13. #define SXLog(...) while(0){}
  14. #endif
  15.  
  16. #endif
  17.  
  18. #define SXFree(p) free(p); p = NULL

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.