Home Help Center

is it legal to put something derived from IceUtil::Shared on the stack

barcodebarcode Member raphael grimmOrganization: Karlsruhe Institute of Technology (KIT)Project: armarx
edited June 2017 in Help Center

hi.
i can remember reading some piece of information saying it is forbidden to put instances from classes derived from IceUtil::Shared on the stack.
of course i don't plan to then in any functions accepting a Handle since this would result in a double free.
now there is a situation where i need this, but i can't find this piece of information anywhere.
i tried it and nothing crashed (yet).
as far as i could see in the code, this should not be a problem.
so just to make sure i want to ask here.
i am using ice 3.5 on ubuntu 14 LTS and use c++.

raphael

Best Answer

  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Answer ✓

    Hi Raphael,

    The heap allocation of class instances is described in the Ice Manual.

    Ice expects that class instances you pass to Ice (through Ptrs) are allocated on the heap: Ice typically acquires a refcount on this object and later releases this refcount, which could result in the destruction of the object (through a delete this). You can also instruct your object to never delete itself with __setNoDelete.

    In case you don't use directly or indirectly the IceUtil::Shared refcounting, you can safely allocate your object on the stack even without calling __setNoDelete.

    Best regards,
    Bernard

Answers

  • bernardbernard Jupiter, FLAdministrators, ZeroC Staff Bernard NormierOrganization: ZeroC, Inc.Project: Ice ZeroC Staff
    Answer ✓

    Hi Raphael,

    The heap allocation of class instances is described in the Ice Manual.

    Ice expects that class instances you pass to Ice (through Ptrs) are allocated on the heap: Ice typically acquires a refcount on this object and later releases this refcount, which could result in the destruction of the object (through a delete this). You can also instruct your object to never delete itself with __setNoDelete.

    In case you don't use directly or indirectly the IceUtil::Shared refcounting, you can safely allocate your object on the stack even without calling __setNoDelete.

    Best regards,
    Bernard

Sign In or Register to comment.