几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量

几何尺寸与公差论坛------致力于产品几何量公差标准GD&T (GDT:ASME)|New GPS(ISO)研究/CAD设计/CAM加工/CMM测量 (http://www.dimcax.com/hust/index.php)
-   DirectDWG (http://www.dimcax.com/hust/forumdisplay.php?f=89)
-   -   【转帖】correct usage of database pointer (http://www.dimcax.com/hust/showthread.php?t=15499)

yang686526 2009-05-04 06:20 PM

【转帖】correct usage of database pointer
 
correct usage of database pointer
correct usage of database pointer
i have a method that loops a some file names and converts the files into dwg files. this code is not correct i don't think.
i think i need a fresh oddbdatabaseptr for each drawing made. at the moment it will just use the same pointer.
please confirm how i should adjust the code to behave correctly without memory errors. thank you.
code:
void cseq2dwgdlg::onbtnconvert()
{
// todo: add your control notification handler code here
int ifile, inumfiles;
cstring strfile, strtext, strword;
cstdiofile fileseq;
csequentialentity entity; // constructor sets entity type as invalid
oddbdatabaseptr pdb;
// loop selected files
inumfiles = m_lbseqfiles.getcount();
for( ifile = 0; ifile < inumfiles; ifile++ )
{
m_lbseqfiles.gettext( ifile, strfile );
fileseq.open( strfile, cfile::moderead );
createdrawing( pdb );
// note that i pass null for database point for now....
while( fileseq.readstring( strtext ) )
{
// trim leading whitespace
strtext.trimleft();
// break off first word
strword = strtext.spanexcluding( _t(" ") );
// are we starting a new entity? if we are, we
// must add the existing entity into the autocad
// file before we start getting data for this new entity.
if( strword == _t("symbol") )
{
// starting a symbol entity
entity.createautocaddata( pdb );
entity.setentitytype( seq_type_symbol );
}
else if( strword == _t("line") )
{
// starting a symbol entity
entity.createautocaddata( pdb );
entity.setentitytype( seq_type_line );
}
else if( strword == _t("region") )
{
// starting a symbol entity
entity.createautocaddata( pdb );
entity.setentitytype( seq_type_region );
}
// now parse the text
entity.parsesequentialtext( strtext );
}
// we must create the last entity object after loop terminates
entity.createautocaddata( pdb );
fileseq.close();
savedrawing( pdb );
}
}
andrew
you can reuse oddbdatabaseptr for a new drawing if you do not need old drawing anymore or if some other smartptr points to it.
smart pointers use object reference counters. if a pointer goes out of the scope (destroyed) or is assigned another value it decrements object reference counter. when reference counter gets zeroed the object is destroyed.
sergey slezkin
thanks for that. i got it working ok at the moment, although i am sticking with 1.10 libraries as those are the only ones i can compile with with having to add global methods to my program (for the dd_alloc methods).
the only thing i am not sure about here is testing when the file is saved. it uses the writefile method, but if the dwg file is open in autocad, it never saves it, and thus i should tell the user this feedback.
andrew


所有的时间均为北京时间。 现在的时间是 02:53 AM.