Shuva's blog
Visual Studio 2008 Express Edition : Free software 
Monday, December 31, 2007, 04:43 AM - Programming
One of the nice things that Microsoft has done is making available a free version of the new Visual Studio 2008 -- the Visual Studio 2008 Express Edition. Though not having the full feature list, it comes with MSDN 2008 and basic functionality to install a version on your PC till you get a full license copy or your back-office completes all formalities to get you some budget :-)

The interesting part is that you may use this free Editions to develop software for commercial use, something that MS never really advocated earlier.

I have not been able to find a list of the precise differences between the Express Edition and the full versions as I have installed them at home last night, but the obvious thing I noticed was that it has limitations on the types of projects you can create. Under the Visual C++ category, it wont allow you to create Smart Device, Test, ATL projects. Setup and Deployment project categories were also missing.

Anyways for basic C++ learning and playing around at home or making a good GUI at home without having to buy software is a cool thing. Check out the FAQ page for more information.

Download it from http://www.microsoft.com/express/product/default.aspx

Hint: If you are looking for the Visual Studio 2005 Express Edition, click on the "not-so-obvious" link called "Previous Version" on the top of the download page.

PS: You will need a decent internet connection to download the entire edition on a DVD -- yes, you can download it all into a DVD(VB, VC++, Web Dev, SQL Server 2005, Popfly). It took me a little more than 45 minutes to just download and install the Visual C++ 2008 (excluding MSDN 2008) on a 256Kbps DSL connection.

Happy new Year 2008.//
1 comment ( 422 views )   |  0 trackbacks   |  permalink   |   ( 3.1 / 42 )
An issue with the SingletonDestroyer class. 
Wednesday, December 26, 2007, 11:30 AM - Programming
In my last post, I had posted a link to a report by John Vlissides, where he illustrates a beautiful way of destructing a Singleton object.

Hoping that you have understood his SingletonDestroyer class, I bring here again the class declaration:

class SingletonDestroyer {
public:
SingletonDestroyer(Singleton* = 0);
~SingletonDestroyer();

void SetSingleton(Singleton* s);
private:
Singleton* _singleton;
};


Recollecting a little more from the article, the Singleton class declares a static SingletonDestroyer member, which gets created automatically at program startup.

And here is the snippet of the Singleton class itself.
    class Singleton {
public:
static Singleton* Instance();
protected:
Singleton() { }

friend class SingletonDestroyer;
virtual ~Singleton() { }
private:
static Singleton* _instance;
static SingletonDestroyer _destroyer;
};

Singleton* Singleton::_instance = 0;
SingletonDestroyer Singleton::_destroyer;

Singleton* Singleton::Instance () {
if (!_instance) {
_instance = new Singleton;
_destroyer.SetSingleton(_instance);
}
return _instance;
}


Now lets come to the point: Note that the constructor of the SingletonDestroyer class takes an optional argument (defaulting to 0) which seems to be pretty fine.

Here is the chain of events that happens most of the time:

1. At startup the SingletonDestroyer is created since it is a static variable.

2. The Singleton object is created somewhere in the code:
Singleton* s = Singleton::Instance();

The Instance() function internally calls _destroyer.SetSingleton(_instance);

Till the end of the program the varibale SingletonDestroyer::_singleton holds the address of the actual singleton object created on the heap.

Now what if the step 1 as mentioned above occurs after step 2. How is this possible?

What if step 2 mentioned above is called outside the main() function of your program. In such a case both the creation of the static variable, SingletonDestroyer variable and the Singleton object are declared in the global space and if they are declared in different files, then there is no guarantee about the sequence in which the two variables would be created.

If step 1 happens after step 2, ie, if the _destroyer variable's constructor gets called after the Singleton object is created, then the SingletonDestroyer::_singleton gets reset to NULL value because of the default argument in the constructor.

If such a thing happens that the destructor of your Singleton object will ever get called at shutdown.

There are two possible solutions to this:

1. Dont create your singleton object outside the main() function. This ensures that the SingletonDestroyer's constructor is always called before the Singleton object is created.

2. Let the constructor of the SingletonDestroyer class have no arguments.


Below is the complete program that illustrates the scenario where the destructor of the Singleton object never gets called.

#include <iostream>

template <class SINGLETON>
class SingletonDestroyer {
public:
SingletonDestroyer(SINGLETON* = 0);
~SingletonDestroyer();

void SetSingleton(SINGLETON*);
private:
// Prevent users from making copies of a
// Destroyer to avoid double deletion:
SingletonDestroyer(const SingletonDestroyer<SINGLETON>&);
void operator=(const SingletonDestroyer<SINGLETON>&);
SINGLETON* _singleton;
};

template <class SINGLETON>
SingletonDestroyer<SINGLETON>::SingletonDestroyer (SINGLETON* s) {
std::cout << "Step 1: Constructor of the SingletonDestroyer object" << std::endl;
_singleton = s;
}

template <class SINGLETON>
SingletonDestroyer<SINGLETON>::~SingletonDestroyer () {
delete _singleton;
}

template <class SINGLETON>
void SingletonDestroyer<SINGLETON>::SetSingleton(SINGLETON* s) {
_singleton = s;
}

class Singleton {
public:
static Singleton* GetInstance();

protected:
Singleton();
~Singleton();

private:
friend class SingletonDestroyer<Singleton>;

static Singleton* _instance;
static SingletonDestroyer<Singleton> _destroyer;
};

Singleton *single = Singleton::GetInstance();

Singleton* Singleton::_instance = NULL;
SingletonDestroyer<Singleton> Singleton::_destroyer;

Singleton* Singleton::GetInstance() {
std::cout << "Step 2: GetInstance : Creation of the Singleton object" << std::endl;
if (!_instance) {
_instance = new Singleton;
_destroyer.SetSingleton(_instance);
}
return _instance;
}

Singleton::Singleton() {
}

Singleton::~Singleton() {
std::cout << "Destructing the Singleton object." << std::endl;
}



int main() {

}



Compiled in Visual Studio 2005, the output is:

Step 2: GetInstance : Creation of the Singleton object
Step 1: Constructor of the SingletonDestroyer object
Press any key to continue . . .


Applying either if the above two suggestions, the output is:

Step 1: Constructor of the SingletonDestroyer object
Step 2: GetInstance : Creation of the Singleton object
Destructing the Singleton object.
Press any key to continue . . .


In this sample program, I had intentionally placed the line


Singleton *single = Singleton::GetInstance();

before

SingletonDestroyer<Singleton> Singleton::_destroyer;

But the point here is that it is perfectly reasonable to have these two lines in different .cpp files or header files and there is no guarantee in what sequence the compiler would consider it.


Happy Destruction.//
add comment ( 249 views )   |  0 trackbacks   |  permalink   |   ( 2.9 / 51 )
Singleton design pattern 
Wednesday, December 19, 2007, 05:17 AM
These days I am more focussed towards design and architecture and have recently bought the book "Patterns of Software Architecture Vol 2". Yes, it the same I had recently suggested in one of my previous blog.

I have been away from blogging for quite some time and was devoting time to my work and maintaining my other blog -- Aminus3 Photo blog.

Something from the old school today. C++ Singleton classes. Here is a simple Logger class that is a singleton.


//Logger.h
class Logger {
private:
Logger(); //private constructor
Logger(const Logger& logger); //private copy constructor.
Logger& operator=(const Logger& logger); //Private assignment operator.
static Logger* m_instance;
public:
static Logger* GetInstance();
void Log(const char*);
};


//Logger.cpp
Logger* Logger::m_instance = NULL;

Logger* Logger::GetInstance() {
if (!m_instance) {
m_instance = new Logger();
}
return m_instance;
}


//Whereever you are using it.
Logger* myLogger = Logger::GetInstance();


Many a times it happens that we create a class and there exists only a single instance of the object in the entire design. Developers are tempted to create a normal class and manually make sure that they will never create a second object. This is a very common practice among many developers. Experience says that over time, either you make a mistake or somebody else ends up managing your code and does not realize the idea of singleton that the original developer had. Simply making the constructor, copy constructor and the assignment operator private or protected leaves a very strong intention of intended design. Apart from this, there are several reasons why you may want to make a class Singleton. Ask a simple question: Does it make sense to have more than once instance of your object?

As far as the question of making constructor, copy constructor and the assignment operator private or protected, ask yourself. Do you think it would ever make sense to derive from this class. If you dont know the answer and there are two many complex stuff that the class is currently doing, play it safe and make it private.

There are several ways of writing the Singleton class. Some developers use a second static boolean variable to keep track of the initial creation of the object.

Another way is to move the m_instance from the class definition into the GetInstance() method as shown below:

//Logger.h
class Logger {
private:
Logger(); //private constructor
Logger(const Logger& logger); //private copy constructor.
Logger& operator=(const Logger& logger); //Private assignment operator.
public:
static Logger* GetInstance();
void Log(const char* c);
};


//Logger.cpp
Logger* Logger::GetInstance() {
static Logger m_instance; //Created only once being static.
return &m_instance;
}


//Whereever you are using it.
Logger* myLogger = Logger::GetInstance();


Why would you want to do this?

In the later case, you dont have to call delete as static objects are cleaned when the application terminates.

In the former case where we used the "new" operator, the implementation is thread safe as the operator "new" is thread safe. But you must clean the object off the free-store by calling "delete" while you end your application. What about object destruction? This is a more complex topic and if you want to get an in depth issues regarding it, I suggest you read C++ the report, "Pattern Hatching" column for June '96 issue by John Vlissides titled "TO KILL A SINGLETON"



If you dont like the idea of always making your constructor, copy constructor and the assignment operator private and want a more simple and elegant way to do the same use the boost::noncopyable. Derive your object from this class and you are good.

Example:
#include <boost/noncopyable.hpp>

class Logger : boost::noncopyable{
public:
static Logger* GetInstance();
void Log(const char* c);
};


Happy Singletonning.//
1 comment ( 92 views )   |  0 trackbacks   |  permalink   |   ( 3.1 / 66 )
One silly/stupid program error that I did 
Thursday, November 29, 2007, 09:01 AM - Programming
I know we all do silly stuff, and what I did today is related to programming(C++). I needed a class where all members were static. No sweat, it worked.

I wanted to reuse some of the code in there and the stupid thing I did was make a derived class from it. It was all working fine, till today when I made another class to derive from it not realizing that all the members in the base class were static. So stupid -- I did not want that.

Having said that it would not be correct to say : Never derive from a class where all data members are static. One shoe does not fit all.
1 comment ( 118 views )   |  0 trackbacks   |  permalink   |   ( 3 / 44 )
Top 10 computer security threats in 2008 
Thursday, November 22, 2007, 03:10 PM - Technology
Come December and your top 10 favorite TV channels are showing the Top 10 events of 2007, be it movies, songs, people, etc etc and blah blah too. There is Top 10 everywhere. McAfee recently released a document that predicts the Top 10 computer security threats for the year 2008.

Get the pdf document here.

Of all of them the most interesting is the one which says "Virtual Threat Growth to Outpace Real-World Growth", which I tend to agree more than all other predictions and this one is probably going to hold true for some more number of years. Security software and systems are becoming more mature everyday but most of the time it is a fight between products of the security software vendors and the hackers. Time has come that these products needs to match the needs of the good guys who have more stake (in terms of money, etc) on online virtual objects. My grandfather had the chance to touch, feel and smell every dime he earned but I will never do so to even 50% of my total earnings in my lifetime. There are(and will be) just virtual objects in software. Security needs to be simplified.

And there is another one in the Top 10 list that bought smiles to my face. It says "Windows Vista Joins the Party".

Happy Top 10-ing.//
1 comment ( 76 views )   |  0 trackbacks   |  permalink   |   ( 3 / 49 )

<<First <Back | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | Next> Last>>