Double free error using sprintf in threaded app
Double free error using sprintf in threaded app
- Subject: Double free error using sprintf in threaded app
- From: Rush Manbert <email@hidden>
- Date: Wed, 15 Apr 2009 12:43:00 -0700
I have a test program that runs this function in multiple threads:
void operator() (void)
{
for (size_t loopCount = 0; loopCount < m_loopCount; ++loopCount)
{
// Do the same thing the socket open code does
char port[sizeof("65536")];
sprintf(port, "%d", m_serverPort);
char buf0[512];
sprintf (buf0, "%s %d", "Our lop cont is: " , loopCount);
char buf1[793];
sprintf (buf1, "Now the loop count is: %d", loopCount);
}
}
I am using boost::thread, but the original app where I saw the error
uses POSIX threads.
If you run this enough times, you eventually see this on the console
output:
multiThreadSprintfTest(62298,0xb038d000) malloc: *** error for object
0x100fe0: double free
*** set a breakpoint in malloc_error_break to debug
and if you are running in the debugger, you get this backtrace:
#0 0x904934a9 in malloc_error_break ()
#1 0x9048e497 in szone_error ()
#2 0x903b8463 in szone_free ()
#3 0x903b82cd in free ()
#4 0x903b7e84 in localeconv_l ()
#5 0x903b3295 in __vfprintf ()
#6 0x903dab86 in sprintf ()
#7 0x00002a68 in TestThread::operator() (this=0x1007dc) at /Users/
rmanbert/testing/multiThreadSprintfTest/main.cpp:27
#8 0x00005ffc in boost::detail::thread_data<TestThread>::run
(this=0x100720) at thread/detail/thread.hpp:56
#9 0x0001bc52 in thread_proxy (param=0x100720) at libs/thread/src/
pthread/thread.cpp:130
#10 0x903e3095 in _pthread_start ()
#11 0x903e2f52 in thread_start ()
The line of code that is executing is the first call to sprintf within
the loop.
The original code comes from a socket I/O library that is converting
an integer port number to the string required for opening the socket.
The library developers use various flavors of Linux and I assume that
they don't see this problem on their systems (they have the library
deployed in a very high usage environment), so I am working on the
assumption that this is Apple-specific.
I was under the impression that sprintf was thread safe. Is this not
the case by design? Is there some workaround that is thread safe?
Any help would be appreciated.
- Rush
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden