|
Boost Users : |
Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to
From: Max (loadcom_at_[hidden])
Date: 2008-12-29 09:58:33
There seems to be a solution that can make the code a little bit
cleaner and easier to maintain.
#define DECLARE_PTR_AUTO_DELETOR(CLASS, VAR_PTR)\
boost::scoped_ptr<CLASS> auto__deletor__for__ ## VAR_PTR (VAR_PTR)
AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;
AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;
DECLARE_PTR_AUTO_DELETOR(AcDbCircle, pFace);
DECLARE_PTR_AUTO_DELETOR(AcDbCirclep, LeftEye);
DECLARE_PTR_AUTO_DELETOR(AcDbCircle, pRightEye);
DECLARE_PTR_AUTO_DELETOR(AcDbArc, pMouth);
// Set the color property.
pFace->setColorIndex (2) ;
pLeftEye->setColorIndex (5) ;
pRightEye->setColorIndex (5) ;
pMouth->setColorIndex (1) ;
// add the entities to the new block table record
if ( (es =pBlockTableRecord->appendAcDbEntity (pFace)) != Acad::eOk ) {
//delete pFace ; // all these 'delete ...' not needed any more ...
//delete pLeftEye ;
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pFace->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pLeftEye)) != Acad::eOk ) {
//delete pLeftEye ;
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pLeftEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pRightEye)) != Acad::eOk ) {
//delete pRightEye ;
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pRightEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pMouth)) != Acad::eOk ) {
//delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pMouth->close () ;
Thanks you all that hepled.
B/Rgds
Max
----- Original Message -----
From: Max <loadcom_at_[hidden]>
To: Emil Dotchevski<emildotchevski_at_[hidden]>
Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to
Date: 2008-12-26 09:22:00
Hello Dotchevski,
Thanks for your explanation.
This question was pushed to me by seeing the following code
in an Autodesk AutoCAD ObjectARX sample program.
Don't pay much attention to the descrete classes, just to the
new/delete operations. I found the if/delete guards to prevent
memory leakage too tedious.
I hope there's a smart_ptr-like technique that could help me
circumvent this verbose coding style.
In other words, my need could be described as:
1) allocate objects dynamically on heap,
2) send them to algorithms that are prone to failure
3) if the opreation succeeds, the objects will be taken care of
by the receiver of these objects, or, otherwise, they will
be automatically freed without having to delete these objects
manually.
AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;
AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;
AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;
// Set the color property.
pFace->setColorIndex (2) ;
pLeftEye->setColorIndex (5) ;
pRightEye->setColorIndex (5) ;
pMouth->setColorIndex (1) ;
// add the entities to the new block table record
if ( (es =pBlockTableRecord->appendAcDbEntity (pFace)) != Acad::eOk ) {
delete pFace ;
delete pLeftEye ;
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pFace->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pLeftEye)) != Acad::eOk ) {
delete pLeftEye ;
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pLeftEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pRightEye)) != Acad::eOk ) {
delete pRightEye ;
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pRightEye->close () ;
if ( (es =pBlockTableRecord->appendAcDbEntity (pMouth)) != Acad::eOk ) {
delete pMouth ;
pBlockTableRecord->erase () ;
pBlockTableRecord->close () ;
return (es) ;
}
pMouth->close () ;
Thanks for your patience!
Best regards
Max
-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(http://space.sina.com.cn/ )
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net