Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question re. usage of "static" within static member functions of aclass

Reply
Thread Tools

question re. usage of "static" within static member functions of aclass

 
 
ssb
Guest
Posts: n/a
 
      09-07-2009
Hi All,
During a code review, I found the following lines of code:

Class Dataublic MyBaseClass
{
Q_OBJECT

Public:
static Data* instance();
~Data();
.
.
.


// NOTE: no static member variables

private:
Data();

}


The "instance" method was implemented as follows:

Data* Data::instance()
{
static Data* model = new Data();
return model;
}


I have never come across a situation where a pointer was set to static
in such a case. Is this valid?What are the potential pitfalls in such
programming practices?

Thanks and Best regards
~ssb
 
Reply With Quote
 
 
 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      09-07-2009
"ssb" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
> During a code review, I found the following lines of code:


[...]

> The "instance" method was implemented as follows:


> Data* Data::instance()
> {
> static Data* model = new Data();
> return model;
> }



> I have never come across a situation where a pointer was set to static
> in such a case. Is this valid?


It's a singleton.




> What are the potential pitfalls in such programming practices?


The storage that `model' points to will never be destroyed, also it's not
thread-safe.

 
Reply With Quote
 
 
 
 
Shrikumar
Guest
Posts: n/a
 
      09-07-2009
Thanks for the quick reply, Chris.
I was wondering about the static pointer part - I have always seen
static variables (that are not pointers) in use, but never a static
pointer (even if it is to guarantee that the singleton always returns
the *same* instance of the Class). Is a static pointer (as in the
instance function) a perfectly valid use of the "static" keyword?

On Sep 7, 10:24*am, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
> "ssb" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed)...
>
> > Hi All,
> > During a code review, I found the following lines of code:

>
> [...]
>
> > The "instance" method was implemented as follows:
> > Data* Data::instance()
> > {
> > * * *static Data* model = new Data();
> > * * *return model;
> > }
> > I have never come across a situation where a pointer was set to static
> > in such a case. Is this valid?

>
> It's a singleton.
>
> > What are the potential pitfalls in such programming practices?

>
> The storage that `model' points to will never be destroyed, also it's not
> thread-safe.


 
Reply With Quote
 
umeshkvguptha umeshkvguptha is offline
Junior Member
Join Date: Sep 2009
Location: Pune,India
Posts: 2
 
      09-07-2009
i didnt Understand the exact attempt your making here to acheive(Singleton? Finalising Derived Class?).

However Code is not Correct in either case and it is Causing Memory leak.

Each and every time when you call Data::instance() Function it creates new Object and Overwrites Static variable with New Object Address.
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-07-2009
On 7 Set, 07:50, Shrikumar <(E-Mail Removed)> wrote:
> On Sep 7, 10:24 am, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
>
> > "ssb" <(E-Mail Removed)> wrote in message

>
> >news:(E-Mail Removed)...

>
> > > Hi All,
> > > During a code review, I found the following lines of code:

>
> > [...]

>
> > > The "instance" method was implemented as follows:
> > > Data* Data::instance()
> > > {
> > > static Data* model = new Data();
> > > return model;
> > > }
> > > I have never come across a situation where a pointer was set to static
> > > in such a case. Is this valid?

>
> > It's a singleton.

>
> > > What are the potential pitfalls in such programming practices?

>
> > The storage that `model' points to will never be destroyed, also it's not
> > thread-safe.

>
> Thanks for the quick reply, Chris.
> I was wondering about the static pointer part - I have always seen
> static variables (that are not pointers) in use, but never a static
> pointer (even if it is to guarantee that the singleton always returns
> the *same* instance of the Class). Is a static pointer (as in the
> instance function) a perfectly valid use of the "static" keyword?


I think it is. After all, that pointer should be initialized only once
and operator new should be called only once - the first time the
method is invoked - but this confirmation should be implied into
Chris' sentence: "It's a singleton".

As usual, there are many ways to achieve the same target while
programming.

But was that coder really meant to implement it that way?

The implementation you reviewed:
-------
Data* Data::instance()
{
static Data* model = new Data();
return model;
}
-------

Gives the same result[1] of:
-------
Data* Data::instance() {
static Data model;
return &model;
}
-------

[1] Well, more or less "the same result". My mod could be preferred
because it doesn't use dynamic memory, but in order to avoid some
client to see the pointer as something that could/should be deleted
sooner or later, the code could return the object as a reference.

Changing the method declaration, it could be implemented it in this
way:
-------
Data& Data::instance() {
static Data model;
return model;
}
-------

or also as:

-------
// data.cpp
#include "data.h"

namespace {
Data model;
}

Data& Data::instance() {
return &model;
}
-------

Which doesn't use the "static" keyword at all.

But of course, being a code-reviewer, you should already know all the
above.

Let's say I'm taking the chance to see if _I_ got it right, showing my
code for review here in clc++

Cheers,
Francesco

PS: please do not top-post the next time, moreover because it messes
up subsequent replies to your top-posting.
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-07-2009

> -------
> // data.cpp
> #include "data.h"
>
> namespace {
> * * Data model;
>
> }
>
> Data& Data::instance() {
> * * *return &model;}
>
> -------


Ops, kill that & before model!
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      09-07-2009
Shrikumar wrote:
> Thanks for the quick reply, Chris.
> I was wondering about the static pointer part - I have always seen
> static variables (that are not pointers) in use, but never a static
> pointer (even if it is to guarantee that the singleton always returns
> the *same* instance of the Class). Is a static pointer (as in the
> instance function) a perfectly valid use of the "static" keyword?

It is valid to declare pointers static if that's what you mean. On a
side note, I think they could avoid both using pointer and the memory
leak (which may be harmless in this case though) as follows:

{
static Data model;
return &model;
}


Hope this will help,
Pavel
>
> On Sep 7, 10:24 am, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
>> "ssb" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed)...
>>
>>> Hi All,
>>> During a code review, I found the following lines of code:

>> [...]
>>
>>> The "instance" method was implemented as follows:
>>> Data* Data::instance()
>>> {
>>> static Data* model = new Data();
>>> return model;
>>> }
>>> I have never come across a situation where a pointer was set to static
>>> in such a case. Is this valid?

>> It's a singleton.
>>
>>> What are the potential pitfalls in such programming practices?

>> The storage that `model' points to will never be destroyed, also it's not
>> thread-safe.

>

 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-07-2009
On Sep 7, 7:13*pm, Francesco <(E-Mail Removed)> wrote:
> > -------
> > // data.cpp
> > #include "data.h"

>
> > namespace {
> > * * Data model;

>
> > }

>
> > Data& Data::instance() {
> > * * *return &model;}

>
> > -------

>
> Ops, kill that & before model!


Heck, no, if that's a singleton the ctor is private, hence the module
cannot instantiate it. Sorry, kill all the above.

Francesco
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-07-2009
On 7 Set, 23:25, Paavo Helde <(E-Mail Removed)> wrote:
> Pavel <dot_com_yahoo@paultolk_reverse.yourself> kirjutas:
>
> > Shrikumar wrote:
> >> Thanks for the quick reply, Chris.
> >> I was wondering about the static pointer part - I have always seen
> >> static variables (that are not pointers) in use, but never a static
> >> pointer (even if it is to guarantee that the singleton always returns
> >> the *same* instance of the Class). Is a static pointer (as in the
> >> instance function) a perfectly valid use of the "static" keyword?

> > It is valid to declare pointers static if that's what you mean. On a
> > side note, I think they could avoid both using pointer and the memory
> > leak (which may be harmless in this case though) as follows:

>
> > {
> > * * *static Data model;
> > * * *return &model;
> > }

>
> This brings along the destruction problems at the end of the program. The
> singleton might be destroyed too early this way, when some code still
> might need access to it. When created dynamically, this problem does not
> occur, and the memory is reclaimed by the OS upon process exit anyway, so
> there is no memory leak anyway. The singleton destructor is not run in
> this case, so one should not put something essential there.


Ahhhrgh! Thanks a lot for pointing this out - I was just stomping on
the same problem with my suggestion.

So then, if I got your post right Paavo: in order to circumvent the
destruction-order problem I should create the singleton instance as a
dynamic object _and_ I should not free it in the destructor -
otherwise I would be throwing in the destruction-order problem again.

Side question - once I'm there - is the following fine?

-------
Data& Data::instance() {
static Data* model = new Data();
return *model;
}
-------

I hope so =/

Best regards,
Francesco
 
Reply With Quote
 
Francesco
Guest
Posts: n/a
 
      09-08-2009
On Sep 8, 8:12*am, Paavo Helde <(E-Mail Removed)> wrote:
> Francesco <(E-Mail Removed)> kirjutas:
>
>
>
> > On 7 Set, 23:25, Paavo Helde <(E-Mail Removed)> wrote:
> >> Pavel <dot_com_yahoo@paultolk_reverse.yourself> kirjutas:

>
> >> > Shrikumar wrote:
> >> >> Thanks for the quick reply, Chris.
> >> >> I was wondering about the static pointer part - I have always seen
> >> >> static variables (that are not pointers) in use, but never a
> >> >> static pointer (even if it is to guarantee that the singleton
> >> >> always returns the *same* instance of the Class). Is a static
> >> >> pointer (as in the instance function) a perfectly valid use of the
> >> >> "static" keyword?
> >> > It is valid to declare pointers static if that's what you mean. On
> >> > a side note, I think they could avoid both using pointer and the
> >> > memory leak (which may be harmless in this case though) as follows:

>
> >> > {
> >> > * * *static Data model;
> >> > * * *return &model;
> >> > }

>
> >> This brings along the destruction problems at the end of the program.
> >> The singleton might be destroyed too early this way, when some code
> >> still might need access to it. When created dynamically, this problem
> >> does not occur, and the memory is reclaimed by the OS upon process
> >> exit anyway, so there is no memory leak anyway. The singleton
> >> destructor is not run in this case, so one should not put something
> >> essential there.

>
> > Ahhhrgh! Thanks a lot for pointing this out - I was just stomping on
> > the same problem with my suggestion.

>
> > So then, if I got your post right Paavo: in order to circumvent the
> > destruction-order problem I should create the singleton instance as a
> > dynamic object _and_ I should not free it in the destructor -
> > otherwise I would be throwing in the destruction-order problem again.

>
> > Side question - once I'm there - is the following fine?

>
> > -------
> > Data& Data::instance() {
> > * * *static Data* model = new Data();
> > * * *return *model;
> > }

>
> Yes I think this is fine, my singletons typically look alike.


Good to know, I suppose the following should be fine too:

-------
Data& Data::instance() {
static Data& model = *(new Data());
// or also, simply:
// static Data& model = *new Data;
return model;
}
-------

I recall someone who wrote "programming _IS_ decision making"

> In addition, in case of multithreaded applications I usually call all
> such instance() methods in the beginning of the program (or in the init
> routine of a dynamically loaded library), in order to avoid potential
> thread clash in the singleton creation. One could attempt to protect the
> singleton creation by a static mutex, but then one would be back at the
> statics destruction order problems.


Thank you for the further details about multi-threading, I'm adding
them to my "toolbox", along with all the good new things I'm learning
here on clc++.

Cheers,
Francesco
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why are member functions allowed to access private data members of aclass? Dwight Army of Champions C++ 19 06-30-2010 06:05 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
AClass() vs. AClass[] constuctors (was Best name for "this method") trans. (T. Onoma) Ruby 6 10-01-2004 11:40 AM
AClass ac=AClass("name",23); VS. AClass ac("name", 23); Alfred E Neuman C++ 3 11-04-2003 10:26 PM



Advertisments