new without delete

P

pauldepstein

The following code for a singleton implementation is recommended in a
website:

class GlobalClass

{

int m_value;

static GlobalClass *s_instance;

GlobalClass(int v = 0)

{

m_value = v;

}

public:

int get_value()

{

return m_value;

}

void set_value(int v)

{

m_value = v;

}

static GlobalClass *instance()

{

if (!s_instance)

s_instance = new GlobalClass;

return s_instance;

}

};



// Allocating and initializing GlobalClass's

// static data member. The pointer is being

// allocated - not the object inself.

GlobalClass *GlobalClass::s_instance = 0;



void foo(void)

{

GlobalClass::instance()->set_value(1);

cout << "foo: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



void bar(void)

{

GlobalClass::instance()->set_value(2);

cout << "bar: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

}



int main(void)

{

cout << "main: global_ptr is " << GlobalClass::instance()->get_value
() << '\n';

foo();

bar();

}


Is the author at fault for the lack of delete statements? Are there
memory-leak issues?

Thanks a lot,

Paul Epstein
 
M

Marcel Müller

The following code for a singleton implementation is recommended in a
website:

class GlobalClass
{ [...]
static GlobalClass *instance()
{
if (!s_instance)
s_instance = new GlobalClass;
return s_instance;
}
};
Is the author at fault for the lack of delete statements? Are there
memory-leak issues?

Well, since it is a singleton it is no memory leak in the way that there
will be memory that is no longer needed. When the application
terminates, all private memory is freed anyway. And before the
application terminates it must not be deleted.

In fact the mentioned pattern uses late initialization which usually is
easier to handle than static object initialization. However, a graceful
cleanup of singletons with dependencies is even more complicated than
the initialization part. So this is simply ignored here.

As long as your objects do not deal with external resources, that are
not released by the operation system, when your application terminates,
this is fine. Otherwise you have a problem with unexpected terminations
anyway.


Marcel
 
A

achhabra

Here the intent is to create single object for the class which when
once init will destroy at the application terminate. Something
suggested by name of the class also. :)

If required to limit the lifetime of the single object created, here
GlobalClass can also maintain instance count as
1. instance() will create object at first call and just increment
count for next calls.
2. Provide Deinit() which decrements count and on instance count == 0
delete's the obj.

-Amit

The following code for a singleton implementation is recommended in a
website:
class GlobalClass
{ [...]
    static GlobalClass *instance()
    {
        if (!s_instance)
          s_instance = new GlobalClass;
        return s_instance;
    }
};
Is the author at fault for the lack of delete statements?  Are there
memory-leak issues?

Well, since it is a singleton it is no memory leak in the way that there
will be memory that is no longer needed. When the application
terminates, all private memory is freed anyway. And before the
application terminates it must not be deleted.

In fact the mentioned pattern uses late initialization which usually is
easier to handle than static object initialization. However, a graceful
cleanup of singletons with dependencies is even more complicated than
the initialization part. So this is simply ignored here.

As long as your objects do not deal with external resources, that are
not released by the operation system, when your application terminates,
this is fine. Otherwise you have a problem with unexpected terminations
anyway.

Marcel
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top