By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Brain-ache on this new operator

P: 39

I've recently seen something happening of which I'm sure is impossible.

I have an application running multi-threaded.
In 2 different threads at roughly exact same time a new class was instantiated using 'new classA(..)'
And both instantiated objects were given the exact same memory address.

I'm having brain-aches at the moment to understand how this is possible.
Can anyone hypothesize why this could happen?

This next is a simple representation of the code:
Expand|Select|Wrap|Line Numbers
  1. class A 
  2. {
  3.    show() { printf( %08X, this );
  4. };
  6. class B
  7. {
  8.    method1() { A* a1 = new A(); a1->show(); }
  9.    method2() { A* a2 = new A(); a2->show(); }
  10. }
  12. main()
  13. {
  14.    B* b1 = new B();
  15.    B* b2 = new B();
  17.    // spawn threads
  18.    if (<thread1>) 
  19.       b1->method1();
  20.    else if (<thread2>)
  21.       b2->method2();
  22. }
In this example code, ive seen it happening that I saw 2 times the same address printed.

This phenomenon is captured in a coredump, where I can clearly see 2 different threads, both in a different area of the code, but both instantiating the same class type on a local variable. After that they call a method on that class, where I see that the this pointers of them are the same.
Dec 7 '10 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,916
How are you spawning the threads?

For example if you are using fork then you are not spawning threads are you you are spawning processes and each process has its own virtual address space and so it would be unreasonable for them to have the same valued address in different virtual address spaces.

A would add data to you class as well and assign it different values and output the value.
Dec 8 '10 #2

P: 39
Hi Banfa,

The threads are spawned using OpenMP we have roughly 9 -> 12 threads running. The instantiations of A occur many times per thread. The main app is running day and night.
The actual class "A" contains many more variables and methods. But let me spare you those details yet. Since I dont want to steer any hypothesis into a certain direction.

In the core dumps I did find that 'a1' got instantiated and performed show() it reached a certain area in that method. lets say by a switch statement. where it crashed because of the following:
Shortly after 'a1', 'a2' got instantiated, (with same address) and show() got invoked. That method reached different area and set a variable to NULL, which made 'a1' crash.
a1->show() had just passed the check whether that variable!=NULL. then performed action on that variable and BOOM.
Dec 8 '10 #3

Expert Mod 5K+
P: 9,197
You create b1 and b2 on the thread with main().

You use b1 and b2 on other threads but do not insure that a prticular thread, say that's using b1, is the only thread that's doing that.

That is, where are your critical sections and communications (events) between main() and your threads?
Dec 8 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.