none
_Destroy(_P) in Vs 2017 RRS feed

  • Question

  • I have one project which developed in Vs 2012 and I am trying to migrate to vs 2017.

    _Destroy(_P) was used in vs 2012 to destroy the resource. but this does not work in vs 2017 and seems that it is deprecated.

    Does anybody know equivalent function of _Destroy(_P) in Vs 2017.

    -Mrutyunjaya

    Friday, September 20, 2019 10:49 AM

All replies

  • Functions in the form _<capital letter> are implementation helper functions. These functions are not part of the C++ standard and as you found out, can be changed or removed at any time. What's more, since they are implementation helper functions, there may be no direct replacement.

    Since you don't mention any specifics, all I can say is that you should use standard functionality to do whatever it is you are trying to do.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, September 20, 2019 5:30 PM
  • template< class T, class _Ax = CHeapAllocatorT< T, CDefaultCppHeap > >

    class CSrvObjectT : public T

    {

    private:

        typedef typename _Ax::template rebind< CSrvObjectT< T > >::other   _Alloc;

        static _Alloc       m_a;

    public:

        typedef T SrvObjectClass;

        //--------------- Constructor / Destructor -----------------------------------

        CSrvObjectT( void ) : T()

            { SetObjectTypeName( typeid( T ).name() + 6 );

              CSvcObjectTable::GetGlobalInstance()->Register( GetISrvObject() );

              hsSvcPRINT( "Created\n" );                              }

        virtual ~CSrvObjectT()

            { HsFinalRelease();

              CSvcObjectTable::GetGlobalInstance()->Unregister( GetISrvObject() );

              hsSvcPRINT( "Destroyed\n" );                              }

        //- - - - - - - - ISrvObject Interface - - - - - - - - - - - - - - - -

        virtual LONG HsAddRef( void )

            { return InternalHsAddRef(); }

        virtual LONG HsRelease( void )

            { LONG lRefCount( InternalHsRelease() );

              if( lRefCount == 0 )

              {

                  _Alloc a;

                  a.destroy( this );

                  a.deallocate( this, 1 );

              }

              return lRefCount;                      }

        virtual CSrvObjectBase* GetObjectBase( void )

            { return static_cast<CSrvObjectBase*>( this ); }

    };

       void destroy(pointer _P)

            {_Destroy(_P); }

    Above is code which I have implemented _Destroy(_P) in vs 2012 but it is deprecated in Vs 2017 what can be alternate for this or how can it be implemented in vs 2017. Can anybody please suggest ?

    Mrutyunjaya

    Saturday, September 21, 2019 11:11 AM
  • Given the context, _Destroy is basically a call to the destructor. To show this in context:

    #include <new>
    #include <cstdio>
    
    template<typename _T>
    struct meh_struct
    {
    	meh_struct() : m_ptr(nullptr)
    	{
    		m_ptr = reinterpret_cast<pointer>(malloc(sizeof(_T)));
    		if (m_ptr)
    		{
    			new(m_ptr) _T{};
    		}
    	}
    
    	~meh_struct()
    	{
    		if (m_ptr)
    		{
    			//The following line is what _Destroy would evaluate to
    			m_ptr->~_T();
    			//The previous line is what _Destroy would evaluate to
    			free(m_ptr);
    			m_ptr = nullptr;
    		}
    	}
    
    	using pointer = _T *;
    
    	pointer m_ptr;
    };
    
    struct my_test
    {
    	my_test()
    	{
    		wprintf(L"In my_test::my_test\n");
    	}
    	~my_test()
    	{
    		wprintf(L"In my_test::~my_test\n");
    	}
    };
    
    int wmain()
    {
    	meh_struct<my_test> my{};
    	return 0;
    }

    Can be seen as an example.

    If you are using an allocator, you can't really use new or delete but you still need to initialise and uninitialise the class. New is replaced by an allocate and placement new and delete is replaced by a direct call to the destructor followed by deallocate.

    My bet is that _Destroy would be implemented similarly to:

    template<typename _T>
    void _Destroy(_T *ptr)
    {
    	ptr->~_T();
    }

    of course, this is just a guess.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Saturday, September 21, 2019 2:56 PM