?

Log in

Previous Entry | Next Entry

Another C++ puzzle

aroach figure this out almost immediately. Assume I have a multi threaded application with the follow code:
class A
{
public:
 A()
 {
    m_ = malloc(10);
 }
 ~A()
 {
   free(m_);
 }

private:
 void* m_;
};

void WorkerThread(void* v)
{
  A a;
  // Line 37
}


What could I place after the comment "// Line 37" to make the code leak 10 bytes of memory for every worker thread I created?

Comments

( 5 comments — Leave a comment )
(Anonymous)
Feb. 11th, 2008 04:53 pm (UTC)
An uninteresting solution:
void *garbage = malloc(10);
chikuru
Feb. 11th, 2008 04:55 pm (UTC)
Re: An uninteresting solution:
Oops -- forgot to log in.

-Chikuru
grieve
Feb. 11th, 2008 06:35 pm (UTC)
Re: An uninteresting solution:
Well technically yes. Let me clarify the question a little more. What line could you add that doesn't use new, malloc, or any of the various calls that allocate memory.
(Anonymous)
Feb. 12th, 2008 05:05 am (UTC)
Re: An uninteresting solution:
At least on Windows:

ExitThread(0);

From MSDN:

ExitThread is the preferred method of exiting a thread in C code. However, in C++ code, the thread is exited before any destructors can be called or any other automatic cleanup can be performed. Therefore, in C++ code, you should return from your thread function.

-DM
grieve
Feb. 13th, 2008 05:05 am (UTC)
Re: An uninteresting solution:
That is the one!
( 5 comments — Leave a comment )

Latest Month

July 2011
S M T W T F S
     12
3456789
10111213141516
17181920212223
24252627282930
31      

Page Summary

Powered by LiveJournal.com
Designed by yoksel