Issue in singleton class

Hi,
As per the standard definition of singleton class, only one object is created for a singleton class and whenever we try to create a new object, same object is returned.

I have tried to develop a small piece of code of the same and as follows.

class singleton;

    int value;
    static singleton obj_singleton;

    function new();
    endfunction

    function void display ();
        $display ("%t %M: value=%0d ",$time, value );
    endfunction

    static function singleton create ();
        if (obj_singleton == null)begin
            obj_singleton = new();
            $display("Creating object ");
            return obj_singleton;
        end
        else $display("Already object exists");
    endfunction

endclass

program ex;
    singleton o_singleton1;
    singleton o_singleton2;

    initial begin
        o_singleton1 = singleton::create();
        o_singleton2 = singleton::create();

        $display ("%t %M: Testing of singleton class",$time );
        o_singleton1.value = 10;
        o_singleton1.display();
        o_singleton2.display();

        o_singleton2.value = 20;
        o_singleton1.display();
        o_singleton2.display();
    end
endprogram

When I try to run the above code, simulator gives error Null pointer dereference in Line o_singleton1.value = 10; .

I know that I have not constructed the class inside initial begin…end in program…endprogram. So it’s illegal to use handle to access the property/method unless property/method.

I can resolve this issue by using property value as static but that is not my intention.

I want to have only object and whenever I change the value of variable “value” in anyone of the object (o_singleton1 and o_singleton2) but actually both are same, both o_singleton1 and o_singleton2 should have the same value according to singleton class definition.

How do I realize this in the above piece of code? Can anyone help me to understand it in better way.

Thank you.

Regards,
Ashwath

In reply to Ashwath Saralaya:

So if obj_singleton is not null, create method will return nothing?
You should change your code like this:

    static function singleton create ();
        if (obj_singleton == null)begin
            obj_singleton = new();
            $display("Creating object ");
        end
        else begin
            $display("Already object exists");
        end
        return obj_singleton;
    endfunction

By the way, in singleton pattern, both obj_singleton in your code and new() function MUST be declared as a local variable, otherwise, your obj_singleton can be changed easily.

In reply to seabeam:

Sorry for late reply…

I tried to implement your suggestions in the existing code. I am facing issue when I declare constructor function as local. i.e

local function new();
endfunction

The simulator gives error as “Lifetime or qualifier(s)‘local’ not allowed before function new declaration”. Can you help me to resolve this issue? Thanking you.

In reply to Ashwath Saralaya:

Make sure you have a recent version of your tool and contact your tool provider if it does not work. Having a local constructor is definitely legal.

In reply to dave_59:

Hi Dave,
I am using irun 10.20 version. Do you think that this is the older version. Anyway I’ll contact them regarding this issue. Thank you.

In reply to Ashwath Saralaya:

I can only comment on Mentor’s Questa/Modelsim tools.

In reply to dave_59:

irun 10.20 is very old. Current version is 14.10.

In reply to shalom:

Thank you all for your valuable inputs.