NSObject * aObj = [[NSObject alloc] init];
[aObj release];
NSLog(@"%@", aObj);
原因
常见的如判断一个字符串是否为空:
if (aString && aString.length > 0) {//...}
@autoreleasepool{
for (int i = 0; i < 100; ++i) {
NSObject * aObj = [[[NSObject alloc] init] autorelease];
//....
}
}
[array addObject:[NSNull null]];
解决办法
if (aObj) {
[array addObject:aObj];
}
@interface NSMutableArray (SafeInsert)
-(void) safeAddObject:(id)anObject;
@end
@implementation NSMutableArray (SafeInsert)
-(void) safeAddObject:(id)anObject {
if (anObject) {
[self addObject:anObject];
}
}
@end
if ([selfrespondsToSelector:@selector(methodNotExist)]) {
[self methodNotExist];
}
四、字节对齐
char *mem = malloc(16); // alloc 16 bytesof data
double *dbl = mem + 2;
double set = 10.0;
*dbl = set;
像上面这段代码,执行到
void *memcpy (void *dstpp, const void *srcpp, size_t len) {
unsigned long int dstp = (long int) dstpp;
unsigned long int srcp = (long int) srcpp;
if (len >= OP_T_THRES) {
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
WORD_COPY_FWD (dstp, srcp, len, len);
}
BYTE_COPY_FWD (dstp, srcp, len);
return dstpp;
}
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) %OPSIZ);
// #define OPSIZ (sizeof(op_t))
// enum op_t
#if PAGE_COPY_THRESHOLD
#include
#define PAGE_COPY_FWD_MAYBE(dstp, srcp,nbytes_left, nbytes) \
do \
{ \
if ((nbytes) >= PAGE_COPY_THRESHOLD && \
PAGE_OFFSET ((dstp) - (srcp)) == 0) \
{ \
/* The amount to copy is past the threshold for copying \
pages virtually with kernel VM operations, and the \
source and destination addresses have the same alignment. */ \
size_t nbytes_before = PAGE_OFFSET (-(dstp)); \
if (nbytes_before != 0) \
{ \
/* First copy the words before the first page boundary. */ \
WORD_COPY_FWD (dstp, srcp,nbytes_left, nbytes_before); \
assert (nbytes_left == 0); \
nbytes -= nbytes_before; \
} \
PAGE_COPY_FWD (dstp, srcp, nbytes_left, nbytes); \
} \
}while (0)
/* The page size is always a power of two,so we can avoid modulo division. */
#define PAGE_OFFSET(n) ((n) & (PAGE_SIZE - 1))
#else
#define PAGE_COPY_FWD_MAYBE(dstp, srcp,nbytes_left, nbytes) /* nada */
#endif
PAGE_COPY_FWD 的宏定义: #definePAGE_COPY_FWD ( dstp,
srcp,
nbytes_left,
nbytes
)
Value:
((nbytes_left) = ((nbytes) - \
(__vm_copy (__mach_task_self (), \
(vm_address_t) srcp, trunc_page(nbytes), \
(vm_address_t) dstp) ==KERN_SUCCESS \
? trunc_page (nbytes) \
: 0)))
#define WORD_COPY_FWD ( dst_bp,
src_bp,
nbytes_left,
nbytes
)
Value:
do \
{ \
if (src_bp % OPSIZ == 0) \
_wordcopy_fwd_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
else \
_wordcopy_fwd_dest_aligned (dst_bp, src_bp, (nbytes) / OPSIZ); \
src_bp += (nbytes) & -OPSIZ; \
dst_bp += (nbytes) & -OPSIZ; \
(nbytes_left) = (nbytes) % OPSIZ; \
} while (0)
char *mem = malloc(16); // alloc 16 bytes of data
double *dbl = mem + 4;
double set = 10.0;
*dbl = set;
References
ARMHacking: EXC_ARM_DA_ALIGN exception
GlibC2.18 memcpy source code
#include
#include
void test(int i) {
void* ap = malloc(1024);
std::cout << ++i << "\n";
test(i);
}
int main() {
std::cout << "start!" << "\n";
test(0);
return 0;
}
- (void)stackOverFlow:(int)i {
char * aLeak = malloc(1024);
NSLog(@"try %d", ++i);
[self stackOverFlow:i];
}
-(void)addPlayer:(Player *)player {
if(player == nil) return;
NSLock* aLock = [[NSLock alloc] init];
[aLock lock];
[players addObject:player];
[aLock unlock];
}
}
- (void)addPlayer:(Player *)player {
if(player == nil) return;
@synchronized(players) {
[players addObject:player];
}
}
/*
* 判断这个Timer不为nil则停止并释放
* 如果不先停止可能会导致crash
*/
#define WVSAFA_DELETE_TIMER(timer) { \
if (timer != nil) { \
[timer invalidate]; \
[timer release]; \
timer = nil; \
}\
}