Problem with Singleton and auto_ptr

S

Soumen

Hi,

I've implemented singleton in one of my project. The way my command
works is user issues
the command and when the command finishes it object gets destroyed.
Singleton is required
to make sure when the command is running, user shouldn't be able to
invoke it again since
this is a GUI application. And my top level class uses auto_ptr. The
interesting part is it always
crashes in the 2nd invocation (after the 1st one is complete) - some
sort of pointer corruption.
If I remove the static pointer for singleton implementation (i.e.
remove the singleton implementation)
itself or change the auto_ptr to normal pointers everything works
fine. I made sure that there's no
transfer of ownership for auto_ptr objects.

Any clue what could be going wrong?

Regards,
~ Soumen
 
P

ppi

Hi,

I've implemented singleton in one of my project. The way my command
works is user issues
the command and when the command finishes it object gets destroyed.
Singleton is required
to make sure when the command is running, user shouldn't be able to
invoke it again since
this is a GUI application. And my top level class uses auto_ptr. The
interesting part is it always
crashes in the 2nd invocation (after the 1st one is complete) - some
sort of pointer corruption.
If I remove the static pointer for singleton implementation (i.e.
remove the singleton implementation)
itself or change the auto_ptr to normal pointers everything works
fine. I made sure that there's no
transfer of ownership for auto_ptr objects.

Any clue what could be going wrong?

Regards,
~ Soumen

maybe you should post some code, like the singleton code ...

-- paulo
 
S

Soumen

maybe you should post some code, like the singleton code ...

-- paulo

Here it goes:
-------------

class MyTypeSTon {
public:
static MyTypeSTon* create();
void addData(const std::string &data);
private:
MyTypeSTon();

typedef std::vector<std::string > ContainerType;
auto_ptr<ContainerType > _container;
static MyTypeSTon* _instance;
}

MyTypeSTon*
MyTypeSTon::create()
{
if (_instance == NULL) {
_instance = new MyTypeSTon();
}

return _instance;
}

MyTypeSTon::MyTypeSTon()
: _container(new ContainerType)
{}

void
MyTypeSTon::addData(const std::string &data)
{
if (_container.get()) {
_container.push_back(data);
}
}
 
G

Goedson Paixao

Here it goes:
-------------

class MyTypeSTon {
public:
    static MyTypeSTon*  create();
    void                addData(const std::string &data);
private:
                        MyTypeSTon();

typedef std::vector<std::string > ContainerType;
auto_ptr<ContainerType >    _container;
static MyTypeSTon*          _instance;

}

MyTypeSTon*
MyTypeSTon::create()
{
   if (_instance == NULL) {
       _instance = new MyTypeSTon();
   }

   return _instance;

}

MyTypeSTon::MyTypeSTon()
:  _container(new ContainerType)
{}

void
MyTypeSTon::addData(const std::string &data)
{
   if (_container.get()) {
       _container.push_back(data);
   }

}

You should have:

MyTypeSTon::~MyTypeSTon() {
_instance = NULL;
}

to make sure _instance never points to an invalid object.
 
S

Soumen

You should have:

MyTypeSTon::~MyTypeSTon() {
_instance = NULL;

}

to make sure _instance never points to an invalid object.

Yes, I'd that in actual code. Here in the example I missed it.
 
S

Soumen

Yes, I'd that in actual code. Here in the example I missed it.

Also, after first pass of my command, everything gets destroyed like
following:

void myCommand() {
auto_ptr<MyTypeSTon> obj(MyTypeSTon::create());

if (obj.get()) {
//Do processing using obj
}
}

But when code re-inters myCommand (not when any object of MyTypeSTon
is already alive)
in the 2nd pass, it crashes - debugging shows _container is corrupted.
If I remove
singleton implementation, everything works.
 
M

Martin York

Also, after first pass of my command, everything gets destroyed like
following:

void myCommand() {
auto_ptr<MyTypeSTon> obj(MyTypeSTon::create());

if (obj.get()) {
//Do processing using obj
}

}

But when code re-inters myCommand (not when any object of MyTypeSTon
is already alive)
in the 2nd pass, it crashes - debugging shows _container is corrupted.
If I remove
singleton implementation, everything works.


Dont do this:
auto_ptr<MyTypeSTon> obj(MyTypeSTon::create());

As the auto_ptr goes out of scope it deletes your singelton.
But it does not reset the MyTypeSTon::_instance variable, thus
subsequent calls to create() will still use this value (now deleted)
without creating a new object.

PS. This is not a standard usage pattern of Singelton.
Even if you fix your code with a destructor as descibed above you will
still have very dangerous code.

What happens if you have two functio copies of your singelton stored
in auto_ptr?
 
M

Martin York

As described by S.Myers in his books (A simplified version).

Singleton:

class MySingelton
{
public:
static MySingelton& instance()
{
static MySingelton instance;
return instance;
}
private:
MySingelton() {}
};
 
S

Soumen

As described by S.Myers in his books (A simplified version).

Singleton:

class MySingelton
{
public:
static MySingelton& instance()
{
static MySingelton instance;
return instance;
}
private:
MySingelton() {}

};

Got it. Yup, this snippet looks much safer than creating object using
new operator inside
instance() method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top