<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - [ObjC] C structures are not cleaned up correctly when passed as an argument in a method called on a nil receiver"
href="https://bugs.llvm.org/show_bug.cgi?id=48207">48207</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>[ObjC] C structures are not cleaned up correctly when passed as an argument in a method called on a nil receiver
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>alxr@fb.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>If a C structure is passed as an argument of an objective-c method called on a
nil receiver, this C structure is not cleaned up correctly (ie., weak pointers
in such C structure are not destroyed via objc_destroyWeak and strong pointers
are not released).
The same issue happens in C++ unless destructor is defined for a structure.
It looks like clang expects the callee (receiver) to clean up, but since
receiver is nil the cleanup never happens.
This change may be related: <a href="https://reviews.llvm.org/D44908">https://reviews.llvm.org/D44908</a>
The following code demonstrates this issue.
$ cat ./main.m
#import <Foundation/Foundation.h>
struct FBStruct {
__weak id<NSObject> weakPtr;
};
@protocol FBProtocol <NSObject>
- (void)doSomething:(struct FBStruct)strct;
@end
static void test(NSObject *obj) {
struct FBStruct strct;
strct.weakPtr = obj;
id<FBProtocol> nilReceiver = nil;
[nilReceiver doSomething:strct]; // Temporary structure never cleaned up
}
int main(int argc, const char * argv[]) {
test([NSObject new]);
return 0;
}
$ clang++ -g -fobjc-arc -fobjc-link-runtime ./main.m
$ ./a.out
objc[28864]: __weak variable at 0x7ffeea0d4850 holds 0x7ffeea0d4870 instead of
0x7faa44409750. This is probably incorrect use of objc_storeWeak() and
objc_loadWeak(). Break on objc_weak_error to debug.
I'm able to reproduce this issue on x86_64 (Mac) and ARM (iPhone 11 Pro)</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>