0

Inside a singleton class's static member function, instead of using getInstance(), I used the static pointer member variable directly.

I did not null check it and it was null at run time, hence null pointer exception.

PC lint did not notify me of this. Usually it notifies me as Prio 2 warning : Possible use of null pointer.

Why did it not notify me?

class stClass
{
    int m_value;
    static stClass *s_instance;
    stClass(int v = 0)
    {
        m_value = v;
    }
  public:
    int get_value()
    {
        return m_value;
    }
    void set_value(int v)
    {
        m_value = v;
    }
    static stClass *getInstance()
    {
        if (!s_instance)
          s_instance = new stClass;
        return s_instance;
    }
    static void intentFunction()
    {
        printf("%d", s_instance->getValue()); // Null pointer exception here...
    }
};
Logesh G
  • 630
  • 1
  • 4
  • 15
  • Maybe a better question (or bugreport?) for the pc-lint people? OTOH, there may be a C++ explanation. – stefaanv Sep 17 '14 at 08:55
  • Maybe because they figured you had a logical answer [**to this question**](http://stackoverflow.com/questions/25719244/why-would-code-explicitly-call-a-static-method-via-a-null-pointer)? – WhozCraig Sep 17 '14 at 08:58
  • @WhozCriag But I'm not calling a static member function. – Logesh G Sep 17 '14 at 13:35

0 Answers0