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
 4 class __declspec(dllexport)  singleton
 5 {
 6 public:
 7     singleton(void);
 8     ~singleton(void);
10     static singleton* instance;
11     static singleton* get_instance();
12 };
// singleton.cpp
16 #include "singleton.h"
17 #include <iostream>
19 singleton* singleton::instance;
21 singleton::singleton(void)
22 {
23 }
25 singleton::~singleton(void)
26 {
27 }
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.


Unknown said...

I have to read this blog..Really nice content and useful for me.Keep update Check out:

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

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

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

priya sri said...

nice post

aws training in Hyderabad

Anonymous said...

Nice blog post,
invideo review

Anonymous said...

if ur interested in learning AWS course visit our website

hamda said...

Many people assume that the seo of a website starts when your website is developed and has content in it. But I would say, you start thinking about SEO right from the point of Purchasing a domain name.