Saturday, June 30, 2012

got multiple singleton instances

We meet a subtle bug while adopting singleton design pattern in a project. The singleton class creates multiple instances.
The image below depicts the dependency relationship between different modules.



The executable depends on two dynamic libraries, dynamic_lib1 and dynamic_lib2. And both dynamic_lib1 and dynamic_lib2 depends on static_lib. There is a singleton class in static_lib. dynamic_lib1 and dynamic_lib2 use get_instance method to retrieve the instance of the singleton class.
The basic skeleton of the singleton class is shown below:

 1 // singleton.h
 2 #pragma once
 3 
 4 class __declspec(dllexport)  singleton
 5 {
 6 public:
 7     singleton(void);
 8     ~singleton(void);
 9 
10     static singleton* instance;
11     static singleton* get_instance();
12 };
13 
14
15
// singleton.cpp
16 #include "singleton.h"
17 #include <iostream>
18 
19 singleton* singleton::instance;
20 
21 singleton::singleton(void)
22 {
23 }
24 
25 singleton::~singleton(void)
26 {
27 }
28 
29 
30 singleton* singleton::get_instance()
31 {
32     // lock here
33     if(!instance)
34     {
35         instance = new singleton();
36     }
37     // unlock here
38     return instance;
39 }


But when we run the executable, we're surprised to find that the the use of get_instance method in dynamic_lib1 and dynamic_lib2 doesn't share the same instance.

After thinking about it carefully, it's clear that the bug is caused by we use static_lib3 as a static library. When we compile dynamic_lib1 and dynamic_lib2, they both link with static_lib, and each get a separate copy of the singleton::instance in data section.

But be aware that the behavior is compiler specific. The singleton is still singleton when I tested with gcc. And the singleton got created multiple instances when I tested microsoft's C++ compiler and apple's developer tools.

11 comments:

Unknown said...

I have to read this blog..Really nice content and useful for me.Keep update Check out: http://www.bestphptraining.in/

Anonymous said...

Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chennai | pmp training institutes in Chennai | pmp training and certification in Chennai

Robotic Process Automation Tutorial said...

Thank you so much for your information,its very useful and helpful to me.Keep updating and sharing. Thank you.
RPA training in chennai | UiPath training in chennai | rpa course in chennai | Best UiPath Training in chennai

Rithi Rawat said...

Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.
artificial intelligence and machine learning course in chennai
machine learning course fees in chennai
machine learning course in chennai
Android training in velachery
PMP training in chennai

Unknown said...

Such a Great Article!! I learned something new from your blog. Amazing stuff. I would like to follow your blog frequently. Keep Rocking!!
Blue Prism training in chennai | Best Blue Prism Training Institute in Chennai

Unknown said...

Thanks for your post. This is excellent information. The list of your blogs is very helpful for those who want to learn, It is amazing!!! You have been helping many application.
best selenium training in chennai | best selenium training institute in chennai selenium training in chennai | best selenium training in chennai | selenium training in Velachery | selenium training in chennai omr | quora selenium training in chennai | selenium testing course fees | java and selenium training in chennai | best selenium training institute in chennai | best selenium training center in chennai

Jack Smith said...

We need to share the important thing that we are the third party and provide independent support service and if you face any issue or trouble during the process you may contact AVG tech support to activate or install AVG products. You just need to fill the required blanks in form and our AVG Support will contact you as quick as possible.

For more information: www.avg.com/retail

123hpcom is an online and remote printer technical support service provider to all models of HP printers including HP Officejet printer, HP Officejet pro printer, HP Envy printer, HP Laserjet printer, HP Photosmart printer, HP Scanjet Printer, HP Deskjet Printer and other printer models across the world like USA, UK, Canada, Australia and other major cities across the world.

For more information: 123.hp.com/setup

malar said...

Thank you for excellent article.You made an article that is interesting.
Tavera car for rent in coimbatore|Indica car for rent in coimbatore|innova car for rent in coimbatore|mini bus for rent in coimbatore|tempo traveller for rent in coimbatore|kodaikanal tour package from chennai

Keep on the good work and write more article like this...

Great work !!!!Congratulations for this blog

Rajesh said...

thanks for sharing this information
aws training in bangalore
Amazon web services training in bangalore
best AWS Training institute in Bangalore
aws certification course in bangalore
devops training in bangalore
devops training institutes in bangalore
devops certification course in bangalore
data science training in bangalore

jose said...

Really nice post. Thank you for sharing amazing information.
Java Training in Credo Systemz/Java Training in Chennai Credo Systemz/Java Training in Chennai/Java Training in Chennai with Placements/Java Training in Velachery/Java Training in OMR/Java Training Institute in Chennai/Java Training Center in Chennai/Java Training in Chennai fees/Best Java Training in Chennai/Best Java Training in Chennai with Placements/Best Java Training Institute in Chennai/Best Java Training Institute near me/Best Java Training in Velachery/Best Java Training in OMR/Best Java Training in India/Best Online Java Training in India/Best Java Training with Placement in Chennai

Benish said...

Really nice post. Thank you for sharing amazing information.
Python training in Chennai/Python training in OMR/Python training in Velachery/Python certification training in Chennai/Python training fees in Chennai/Python training with placement in Chennai/Python training in Chennai with Placement/Python course in Chennai/Python Certification course in Chennai/Python online training in Chennai/Python training in Chennai Quora/Best Python Training in Chennai/Best Python training in OMR/Best Python training in Velachery/Best Python course in Chennai