Opened 4 years ago

Closed 4 years ago

#16638 closed bug (fixed)

ObjectDeleter: size in twice bigger then regular pointer

Reported by: X512 Owned by: nobody
Priority: normal Milestone: Unscheduled
Component: - General Version: R1/Development
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description

This is hrev54752.

AutoDeleter use DeleteFunc fDelete field. C++ don't like zero field size and adjust it to 1 byte. After alignment ObjectDeleter size become 2*sizeof(void*). See test code.

There are no such problem in std::unique_ptr. It use some C++ tricks that need to be investigated and reused in AutoDeleter.h.

Attachments (1)

PtrSize.cpp (401 bytes ) - added by X512 4 years ago.

Download all attachments as: .zip

Change History (6)

by X512, 4 years ago

Attachment: PtrSize.cpp added

comment:1 by X512, 4 years ago

Test program output:

sizeof(ptr1): 8
sizeof(ptr2): 16
sizeof(ptr3): 8

comment:2 by X512, 4 years ago

Following trick can fix issue:

  • headers/private/shared/AutoDeleter.h

    diff --git a/headers/private/shared/AutoDeleter.h b/headers/private/shared/AutoDeleter.h
    index 7f16091809..b77f007185 100644
    a b template<typename C, typename DeleteFunc>  
    2929class AutoDeleter {
    3030public:
    3131    inline AutoDeleter()
    32         : fObject(NULL)
     32        : fInt(NULL)
    3333    {
    3434    }
    3535
    3636    inline AutoDeleter(C *object)
    37         : fObject(object)
     37        : fInt(object)
    3838    {
    3939    }
    4040
    4141    inline ~AutoDeleter()
    4242    {
    43         fDelete(fObject);
     43        fInt(fInt.fObject);
    4444    }
    4545
    4646    inline void SetTo(C *object)
    4747    {
    48         if (object != fObject) {
    49             fDelete(fObject);
    50             fObject = object;
     48        if (object != fInt.fObject) {
     49            fInt(fInt.fObject);
     50            fInt.fObject = object;
    5151        }
    5252    }
    5353
    public:  
    6363
    6464    inline C *Get() const
    6565    {
    66         return fObject;
     66        return fInt.fObject;
    6767    }
    6868
    6969    inline C *Detach()
    7070    {
    71         C *object = fObject;
    72         fObject = NULL;
     71        C *object = fInt.fObject;
     72        fInt.fObject = NULL;
    7373        return object;
    7474    }
    7575
    7676    inline C *operator->() const
    7777    {
    78         return fObject;
     78        return fInt.fObject;
    7979    }
    8080
    8181protected:
    82     C           *fObject;
    83     DeleteFunc  fDelete;
     82    struct Internal: DeleteFunc {
     83        C* fObject;
     84        inline Internal(C *object): fObject(object) {}
     85    } fInt;
    8486
    8587private:
    8688    AutoDeleter(const AutoDeleter&);

comment:3 by X512, 4 years ago

Similar problem with CObjectDeleter, but it actually store pointer to destructor function. I suggest interface change from CObjectDeleter<Type, DestructorResult> ptr(val, destructor) to CObjectDeleter<Type, DestructorResult, destructor> ptr(val). It will also allow to declare typedef for specific pointer type with destructor function.

comment:4 by X512, 4 years ago

Fixed in hrev54773.

comment:5 by waddlesplash, 4 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.