Why does this code crash?
#include <iostream>
#include <functional>
int main(int argc, const char * argv[])
{
std::function<void(int)> function = [](int)
{
};
auto binding = std::bind(function, 10);
std::function<void()> jobFunctor = binding; // crashes here with EXC_BAD_ACCESS
return 0;
}
When converting the result of the bind to jobFunctor
there is an infinite stack recursion in a std::function
constructor.
I am running Mac OS X 10.8.5, I compile this code with Xcode 5.0.2 using libc++, the compiler version:
LO50F-04-198BX:$ clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
I'm fairly sure that this code should work, and that it is a defect in the implementation of libc++.
I've posted a bug report for you on the libc++ bug tracker. http://llvm.org/bugs/show_bug.cgi?id=18282
Edit: And as Casey has pointed out, this has already been fixed in libc++. Now you'll simply need to wait for Apple to release an updated version of libc++ in some forthcoming version of XCode / OS X.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With