VirtualBox

Changeset 78756 in vbox for trunk/include/VBox


Ignore:
Timestamp:
May 26, 2019 2:59:24 AM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
130843
Message:

VBox/com/ptr.h,VBox/com/microatl.h: Catch all bad_alloc exceptions in createObject and CreateInstance and turn them into E_OUTOFMEMORY return codes. This greatly simplifies exception analysis in code using these two methods.

Location:
trunk/include/VBox/com
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/microatl.h

    r76585 r78756  
    10501050
    10511051        HRESULT hrc = E_OUTOFMEMORY;
    1052         CComObject<Base> *p = new(std::nothrow) CComObject<Base>();
     1052        CComObject<Base> *p = NULL;
     1053        try
     1054        {
     1055            p = new CComObject<Base>();
     1056        }
     1057        catch (std::bad_alloc &)
     1058        {
     1059            p = NULL;
     1060        }
    10531061        if (p)
    10541062        {
    10551063            p->InternalFinalConstructAddRef();
    1056             hrc = p->_AtlInitialConstruct();
    1057             if (SUCCEEDED(hrc))
    1058                 hrc = p->FinalConstruct();
     1064            try
     1065            {
     1066                hrc = p->_AtlInitialConstruct();
     1067                if (SUCCEEDED(hrc))
     1068                    hrc = p->FinalConstruct();
     1069            }
     1070            catch (std::bad_alloc &)
     1071            {
     1072                hrc = E_OUTOFMEMORY;
     1073            }
    10591074            p->InternalFinalConstructRelease();
    10601075            if (FAILED(hrc))
  • trunk/include/VBox/com/ptr.h

    r76585 r78756  
    469469     *  object doesn't increase the lock count of the server module, as it
    470470     *  does otherwise.
     471     *
     472     *  @note In order to make it easier to use, this method does _not_ throw
     473     *        bad_alloc, but instead returns E_OUTOFMEMORY.
    471474     */
    472475    HRESULT createObject()
    473476    {
    474         HRESULT rc;
     477        HRESULT hrc;
    475478#ifndef VBOX_WITH_XPCOM
    476479# ifdef VBOX_COM_OUTOFPROC_MODULE
    477         ATL::CComObjectNoLock<T> *obj = new ATL::CComObjectNoLock<T>();
     480        ATL::CComObjectNoLock<T> *obj = NULL;
     481        try
     482        {
     483            obj = new ATL::CComObjectNoLock<T>();
     484        }
     485        catch (std::bad_alloc &)
     486        {
     487            obj = NULL;
     488        }
    478489        if (obj)
    479490        {
    480491            obj->InternalFinalConstructAddRef();
    481             rc = obj->FinalConstruct();
     492            try
     493            {
     494                hrc = obj->FinalConstruct();
     495            }
     496            catch (std::bad_alloc &)
     497            {
     498                hrc = E_OUTOFMEMORY;
     499            }
    482500            obj->InternalFinalConstructRelease();
     501            if (FAILED(hrc))
     502            {
     503                delete obj;
     504                obj = NULL;
     505            }
    483506        }
    484507        else
    485             rc = E_OUTOFMEMORY;
     508            hrc = E_OUTOFMEMORY;
    486509# else
    487510        ATL::CComObject<T> *obj = NULL;
    488         rc = ATL::CComObject<T>::CreateInstance(&obj);
     511        hrc = ATL::CComObject<T>::CreateInstance(&obj);
    489512# endif
    490513#else /* VBOX_WITH_XPCOM */
    491         ATL::CComObject<T> *obj = new ATL::CComObject<T>();
     514        ATL::CComObject<T> *obj;
     515        try
     516        {
     517            obj = new ATL::CComObject<T>();
     518        }
     519        catch (std::bad_alloc &)
     520        {
     521            obj = NULL;
     522        }
    492523        if (obj)
    493             rc = obj->FinalConstruct();
     524        {
     525            try
     526            {
     527                hrc = obj->FinalConstruct();
     528            }
     529            catch (std::bad_alloc &)
     530            {
     531                delete obj;
     532                obj = NULL;
     533                hrc = E_OUTOFMEMORY;
     534            }
     535        }
    494536        else
    495             rc = E_OUTOFMEMORY;
     537            hrc = E_OUTOFMEMORY;
    496538#endif /* VBOX_WITH_XPCOM */
    497539        *this = obj;
    498         return rc;
     540        return hrc;
    499541    }
    500542};
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette