0

I've got some problem with static pointers and variables in Bada. First I tried to create singleton class and did something like this: Header:

    static Session *getInstanceOf();
private:
    static Session *instance;

Source:

Session* Session::getInstanceOf(){
    if (instance==NULL){
        instance=new Session();}
    return instance;
}

But application crashed without any error message. Then I tried creating static class field and returning it by:

ArrayList* User::GetUniv()
{
    return &Universities;
}

But it had the same result. Do you have any idea why is it so? Thanks for any help.

jimmy
  • 23
  • 4
  • Not enough information. Are you using multiple threads? Also, this code leaks. – Konrad Rudolph Apr 25 '12 at 15:29
  • Regarding your singleton solution: Did you initialize your Session pointer to NULL? Also where did it crash? Within the getInstanceOf function, or when you use the returned ptr, etc. ? – lx. Apr 25 '12 at 15:30

3 Answers3

1

Are you initialising Session::instance in your implementation file? Like this:

Session* Session::instance = NULL;

Edit: Also consider the static initialization order fiasco.

Michael Wild
  • 24,977
  • 3
  • 43
  • 43
1

You can do like this

ArrayList* User::GetUniv()
{
    static Universities;
    return &Universities;
}
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44
0

This is somewhat dangerous:

if (instance==NULL){
    instance=new Session();}

Because your instance doesn't appear to have been initialized to NULL. Pointers may have a garbage value when uninitialized.

Benj
  • 31,668
  • 17
  • 78
  • 127