Hi,
> You should call ar.Close() BEFORE mf.Detach(), to ensure that data is
> written out to the 'file' before trying to access the memory...
Great, that fixed the problem, thank you very much! :-)
I also found that I need to flush the archive and file buffers, otherwise
GetLength() returns incorrect values.
i.e.
ar.Flush(); // flush the archive first
mf.Flush(); // then the file
Thanks again,
Carlos
----- Original Message -----
From: "Jason Teagle" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, December 16, 2003 8:37 PM
Subject: RE: [msvc] Problem with serialization
> >I have another question though:
> >
> >CMemFile mf;
> > CArchive ar( &mf, CArchive::store );
> > COleServerDoc::Serialize(ar);
> > mf.SeekToBegin();
> > int nSize = mf.GetLength();
> > BYTE* pBuffer = mf.Detach();
> > CString csBase64;
> > csBase64 = CMarkup::EncodeBase64( pBuffer, nSize);
> >
> > xml.AddChildElem("Values", csBase64);
> > ar.Close();
> >
> >This asserts when the memory file object (mf) gets out of scope or when
> >ar.Close() is called. The assertion is in CMemFile::AssertValid(),
exactly
> in the following line:
> >
> >ASSERT(m_nFileSize <= m_nBufferSize);
> >
> >Any idea?
>
> You should call ar.Close() BEFORE mf.Detach(), to ensure that data is
> written out to the 'file' before trying to access the memory... otherwise
> you're "whipping the carpet out from underneath your feet". I believe this
> is why ar.Close() causes the assert. I believe mf going out of scope
causes
> an assert if done before ar.Close() because it implicitly calls
mf.Close(),
> and in this situation the CMemFile is 'hooked' open by the CArchive, and
> thus must be released by the archive before you try and close the 'file'.
>
> It might seem wrong to close the archive before accessing the memory in
> CMemFile, but it is not; closing the archive simply releases the hold on
the
> 'file', it does not close it in any way (I'm guessing you were worried it
> would close and destroy the CMemFile if you closed the archive before
> Detach()?).
>
> --
> Jason Teagle
> [EMAIL PROTECTED]
>
>
>
> _______________________________________________
> msvc mailing list
> [EMAIL PROTECTED]
> See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for
subscription changes, and list archive.