stream and BRepTools::Write and Read

The following code :

TopoDS_Shape _objet;
const char* fichier;
std::ofstream flux;;
BRepTools::Write(_objet, flux);

Seams to work fine in Release mode but crash in Debug mode with OpenCascade 5.2.
Does somebody get the same problem ?
Reading the source code I do not understand why.
Any idea ?

Hugues's picture


I got troubles too using STL stream with this function. I was obliged to consider the other overload, i.e. :
BRepTools::Write (const TopoDS_Shape& sh, const Standard_CString file).

colorviz's picture

Hi Dominique ! ;-)

Are you using debug version of Cascade libs and dlls ?

Got the same problem (works in release / crashes in debug), and solved this issue by linking my debug exe with debug libs and using debug dlls instead of release ones of cascade distribution.

Since 5.2, OpenCascade no more delivers debug w32 libs and dlls.

Hope it'll help,

Sas's picture

Hi Francois!
I have the same problem as urs. i am new to vc++, what is the difference between debug version of Cascade lis and dlls and ordinary one which is available? How to create the debug version of libs and dlls.

and how to link that dlls and libs?

pls help me with this.


Fabio Napodano's picture

just for the record, we use this function to work around the problem, in order to use the release version of OCC under our debug project (otherwise it would bee way too slow to be usable)

void CFileManager::WriteShape(const TDF_Label &lab, stringstream &stream)
TopoDS_Shape shape = COcafTools::GetShape(lab);

#ifdef _DEBUG

Standard_CString sFile = "c:\\temp.brep";

filebuf fb;,ios::in);
Standard_IStream streamTmp(&fb);

streamTmp.seekg (0, ios::end);
Standard_Integer len = streamTmp.tellg();
streamTmp.seekg (0, ios::beg);

char *buff = new char[len+1];
stream << buff << "\n";
delete [] buff;

char *cTmp = new char[4096];
if(streamTmp.eof() || streamTmp.bad())
delete[] cTmp;
stream << cTmp << "\n";
delete[] cTmp;




note that, on our first test with OCC6.3, the BRepTools::Write seem to crash even in release mode (I didn't try myself, but a workmate told me so), so it could be needed to use the #ifdef _DEBUG version on this code for release too if you are using OCC6.3

Fabio Napodano's picture

forgot to add the read function:

Standard_Boolean CFileManager::ReadShape(TopoDS_Shape &shape, stringstream &stream)
BRep_Builder brepb;

#ifdef _DEBUG

Standard_CString sFile = "c:\\temp.brep";
filebuf fb;,ios::out);

Standard_OStream streamTmp(&fb);
streamTmp << stream.str().c_str();


return BRepTools::Read(shape,sFile,brepb);



return shape.IsNull();