gcc/llvm-gcc can call a destructor before the object is used in a block

Originator:rafael.espindola
Number:rdar://10007196 Date Originated:23-Aug-2011 12:25 PM
Status:open Resolved:
Product: Product Version:
Classification: Reproducible:
 
23-Aug-2011 12:25 PM Rafael Espindola:
Given

struct foo {
  foo();
  ~foo();
  foo(const foo&);
  void bar() const;
};
typedef void (^bar)();
void g(bar x);
void f() {
  foo x;
  g(^{x.bar();});
}

llvm-gcc produces the following IL

define internal void @__f_block_invoke_1(%struct.__block_literal_1* %.block_descriptor) nounwind optsize ssp {
entry:
  %x = alloca %struct.foo, align 8                ; <%struct.foo*> [#uses=3]
  %0 = getelementptr inbounds %struct.__block_literal_1* %.block_descriptor, i64 0, i32 5 ; <%struct.foo*> [#uses=1]
  call void @_ZN3fooC1ERKS_(%struct.foo* %x, %struct.foo* %0) nounwind
  call void @_ZN3fooD1Ev(%struct.foo* %x) nounwind
  call void @_ZNK3foo3barEv(%struct.foo* %x) nounwind
  ret void
}

Note the destructor being called on x just before it is passed to bar. Testing shows that gcc-4.2 has a similar bug.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!