1 // C, C++ test()2void test() { 3 int *p = (int *)malloc(sizeof(int)); 4 delete p; // warn 5 } 6 7 // C, C++ 8 void __attribute((ownership_returns(malloc))) *user_malloc(size_t); 9 test()10void test() { 11 int *p = (int *)user_malloc(sizeof(int)); 12 delete p; // warn 13 } 14 15 // C, C++ test()16void test() { 17 int *p = new int; 18 free(p); // warn 19 } 20 21 // C, C++ test()22void test() { 23 int *p = new int[1]; 24 realloc(p, sizeof(long)); // warn 25 } 26 27 // C, C++ 28 template <typename T> 29 struct SimpleSmartPointer { 30 T *ptr; 31 SimpleSmartPointerSimpleSmartPointer32 explicit SimpleSmartPointer(T *p = 0) : ptr(p) {} ~SimpleSmartPointerSimpleSmartPointer33 ~SimpleSmartPointer() { 34 delete ptr; // warn 35 } 36 }; 37 test()38void test() { 39 SimpleSmartPointer<int> a((int *)malloc(4)); 40 } 41 42 // C++ test()43void test() { 44 int *p = (int *)operator new(0); 45 delete[] p; // warn 46 } 47 48 // Objective-C, C++ test(NSUInteger dataLength)49void test(NSUInteger dataLength) { 50 int *p = new int; 51 NSData *d = [NSData dataWithBytesNoCopy:p 52 length:sizeof(int) freeWhenDone:1]; 53 // warn +dataWithBytesNoCopy:length:freeWhenDone: cannot take 54 // ownership of memory allocated by 'new' 55 } 56 57