AcDbObjectPointer - non-database resident objects?

AcDbObjectPointer - non-database resident objects?

Anonymous
Not applicable
1,327 Views
10 Replies
Message 1 of 11

AcDbObjectPointer - non-database resident objects?

Anonymous
Not applicable
Is it valid to use the AcDbObjectPointer class for non-database resident entities??

Mike B
0 Likes
1,328 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
Open dbobjptr.h, and look at the definition of
the closeInternal() function, and see if you can
come up with the answer.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:[email protected]...
Is it valid to use the AcDbObjectPointer class for non-database resident entities??

Mike B
0 Likes
Message 3 of 11

Anonymous
Not applicable
Well, based on the following code:
[code]
if (m_ptr->objectId().isNull()) {
delete m_ptr;
es = Acad::eOk;
}
[/code]
I would say it is OK. Well, this leads me to ask the following question:

If I define an object array:
[code]
typedef AcArray MY_OBJECT_ARRAY;
[/code]
Then in a class I declare:
[code]
class CSomeClass
{
MY_OBJECT_ARRAY m_objs;

public:
int AddObject(CMyObject*)
{
m_objs.appen(CMyObject);
return m_objs.length();
}
};
[/code]
Then I create a smat pointer and add it to the array, knowing that a smart pointer deletes itself when out of scope, what will happen here?
[code]
void CMyOtherClass::SomeFunc()
{
AcDbObjectPointer pObj;
if(pObj.create() == Acad::eOk)
{
m_SomeClass.AddObject(pObj);
}
}
[/code]
When will the object be deleted?

Mike B
0 Likes
Message 4 of 11

Anonymous
Not applicable
Well, I believe the life of the objects will be until the array is cleared, here is the problem:

I am creating a title block entity for my drawing, I have sub-entities such as information tables. I add two tables to the title block, but when worldDraw(...) is called, I notice that both elements in the array have the same properties although I specifically set them differently:

[code]
void CMainUI::InitDrawing()
{
acDocManager->lockDocument(curDoc(), AcAp::kWrite);
Setup();

AcDbObjectPointer pTitleBlock;
if(pTitleBlock.create() == Acad::eOk)
{
AcDbObjectId objId = AcDbObjectId::kNull;
if(DocVars.docData().PostToDatabase(objId, pTitleBlock) != Acad::eOk)
{
::AfxMessageBox(_T("Error occured attempting to create title block!"), MB_OK|MB_ICONSTOP);
return;
}

DocVars.docData().SetTitleBlockId(objId);

AcDbExtents ext;
ext.set(AcGePoint3d(0.125, 0.125, 0), AcGePoint3d(2.5625, 0.875,0));
pTitleBlock->AddInfoTable(_T("Strands"), ext);

ext.set(AcGePoint3d(2.5625, 0.125,0), AcGePoint3d(6.625, 1.625, 0));
pTitleBlock->AddInfoTable(_T("Hardware"), ext);

ext = pTitleBlock->GetPageExtents();
ZoomExtents(ext);
}
acDocManager->unlockDocument(curDoc());
}

// Here is the AddInfoTable(...)
int CTitleBlock::AddInfoTable(const AcString& acsIdentifier, const AcDbExtents& ext)
{
AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);

m_infoTables.append(pTable);
}

return m_infoTables.length() - 1;
}
[/code]
Any ideas or suggestions?

Mike B
0 Likes
Message 5 of 11

Anonymous
Not applicable
I checked the m_ptr of the objects after create() was called and for some reason they both point to the same address?

Why??

Mike B
0 Likes
Message 6 of 11

Anonymous
Not applicable
Mike:

> Then I create a smat pointer and add it to the array...

A "smart pointer" is an object. You can't add a smart pointer object to
an array of pointers to a different class. I suggest you single step
through your code so you can learn what it is doing. 🙂
--
Owen Wengerd
President, ManuSoft ==> http://www.manusoft.com
VP Americas, CADLock, Inc. ==> http://www.cadlock.com
0 Likes
Message 7 of 11

Anonymous
Not applicable
The life of the wrapped AcDbObject* is the scope
of the AcDbObjectPointer variable:

AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);
m_infoTables.append(pTable);

} // life of AcDbObject ends here.

The problem is that your AcDbObjectPointer goes
out of scope at the closing brace, and that is where
the life of the wrapped AcDbObject ends. Hence,
that's where the AcDbObject is deleted if it was not
added to the database, or closed if it was added to
the database. In either case the wrapped AcDbObject*
pointer is no longer usable after that.

One way to solve the problem is to call release()
on the AcDbObjectPointer variable, and then you
need to close() or delete the pointers in the array
manually after you're done.

AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);
CInfoTable* pObj;
pTable.release(pObj);
m_infoTables.append(pObj);
}

And if the AcDbObjects were appended to the
database, then at some point after that you
would need to do something like this:

for( int i = 0; i < m_InfoTables.length(); i++ )
m_InfoTables->close();

Or if the pointers were not added to the database,
then:

for( int i = 0; i < m_InfoTables.length(); i++ )
delete (CInfoTable*) m_InfoTables;


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:[email protected]...
Well, I believe the life of the objects will be until the array is cleared, here is the problem:

I am creating a title block entity for my drawing, I have sub-entities such as information tables. I add two tables to the title block, but when worldDraw(...) is called, I notice that both elements in the array have the same properties although I specifically set them differently:

[code]
void CMainUI::InitDrawing()
{
acDocManager->lockDocument(curDoc(), AcAp::kWrite);
Setup();

AcDbObjectPointer pTitleBlock;
if(pTitleBlock.create() == Acad::eOk)
{
AcDbObjectId objId = AcDbObjectId::kNull;
if(DocVars.docData().PostToDatabase(objId, pTitleBlock) != Acad::eOk)
{
::AfxMessageBox(_T("Error occured attempting to create title block!"), MB_OK|MB_ICONSTOP);
return;
}

DocVars.docData().SetTitleBlockId(objId);

AcDbExtents ext;
ext.set(AcGePoint3d(0.125, 0.125, 0), AcGePoint3d(2.5625, 0.875,0));
pTitleBlock->AddInfoTable(_T("Strands"), ext);

ext.set(AcGePoint3d(2.5625, 0.125,0), AcGePoint3d(6.625, 1.625, 0));
pTitleBlock->AddInfoTable(_T("Hardware"), ext);

ext = pTitleBlock->GetPageExtents();
ZoomExtents(ext);
}
acDocManager->unlockDocument(curDoc());
}

// Here is the AddInfoTable(...)
int CTitleBlock::AddInfoTable(const AcString& acsIdentifier, const AcDbExtents& ext)
{
AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);

m_infoTables.append(pTable);
}

return m_infoTables.length() - 1;
}
[/code]
Any ideas or suggestions?

Mike B
0 Likes
Message 8 of 11

Anonymous
Not applicable
Sorry, the snippet showing the scope of the variable is wrong.

more correctly:

{
AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);
m_infoTables.append(pTable);
}

} // life of the wrapped AcDbObject ends here.


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:[email protected]...
Well, I believe the life of the objects will be until the array is cleared, here is the problem:

I am creating a title block entity for my drawing, I have sub-entities such as information tables. I add two tables to the title block, but when worldDraw(...) is called, I notice that both elements in the array have the same properties although I specifically set them differently:

[code]
void CMainUI::InitDrawing()
{
acDocManager->lockDocument(curDoc(), AcAp::kWrite);
Setup();

AcDbObjectPointer pTitleBlock;
if(pTitleBlock.create() == Acad::eOk)
{
AcDbObjectId objId = AcDbObjectId::kNull;
if(DocVars.docData().PostToDatabase(objId, pTitleBlock) != Acad::eOk)
{
::AfxMessageBox(_T("Error occured attempting to create title block!"), MB_OK|MB_ICONSTOP);
return;
}

DocVars.docData().SetTitleBlockId(objId);

AcDbExtents ext;
ext.set(AcGePoint3d(0.125, 0.125, 0), AcGePoint3d(2.5625, 0.875,0));
pTitleBlock->AddInfoTable(_T("Strands"), ext);

ext.set(AcGePoint3d(2.5625, 0.125,0), AcGePoint3d(6.625, 1.625, 0));
pTitleBlock->AddInfoTable(_T("Hardware"), ext);

ext = pTitleBlock->GetPageExtents();
ZoomExtents(ext);
}
acDocManager->unlockDocument(curDoc());
}

// Here is the AddInfoTable(...)
int CTitleBlock::AddInfoTable(const AcString& acsIdentifier, const AcDbExtents& ext)
{
AcDbObjectPointer pTable;
if(pTable.create() == Acad::eOk)
{
pTable->SetExtents(ext);
pTable->SetIdentifier(acsIdentifier);

m_infoTables.append(pTable);
}

return m_infoTables.length() - 1;
}
[/code]
Any ideas or suggestions?

Mike B
0 Likes
Message 9 of 11

Anonymous
Not applicable
Thanks for the replies everyone, very helpful!

Mike B
0 Likes
Message 10 of 11

Anonymous
Not applicable
"Owen Wengerd" wrote:

>> A "smart pointer" is an object. You can't
>> add a smart pointer object to an array of
>> pointers to a different class.

Hi Owen. I don't think he was doing that.

AcArray objArray;
AcDbObjectPointer myObject;
myObject.create();
objArray.append(myObject);

Since AcArray has:

AcArray::append(const T& value);

And AcDbObjectPointer has:

AcDbObjectPointer::operator const T_OBJECT*() const;

append() gets the wrapped object implicitly. 🙂

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

"Owen Wengerd" wrote in message news:[email protected]...
Mike:

> Then I create a smat pointer and add it to the array...

A "smart pointer" is an object. You can't add a smart pointer object to
an array of pointers to a different class. I suggest you single step
through your code so you can learn what it is doing. 🙂
--
Owen Wengerd
President, ManuSoft ==> http://www.manusoft.com
VP Americas, CADLock, Inc. ==> http://www.cadlock.com
0 Likes
Message 11 of 11

Anonymous
Not applicable
Tony:

> I don't think he was doing that.

I know he wasn't actually doing that, but he *thought* he was adding a
smart pointer object to the array. That was why I told him to step through
his code so he could see what he was actually doing. I purposely didn't
explain it so that it could be left as an exercise for the OP. 🙂
--
Owen Wengerd
President, ManuSoft ==> http://www.manusoft.com
VP Americas, CADLock, Inc. ==> http://www.cadlock.com
0 Likes