CString Class Research (3)
4. CStringData Allocate and Dispose.
In Prior Section, WE KNOW The CStringData Has Two Parts, ONE IS Header, Include 3 Member Variables and 1 Member Function, Totally 12 Bytes, And The Other Part IS Actual Data.
Look The Following Code in Mfc To Allocate and Dispose CstringData And ITS Data Buffer.
CStringData * PDATA;
PDATA = (CStringData *)
New Byte [Sizeof (CSTRINGDATA) (Nlen 1) * Sizeof (tchar)];
PDATA-> Nalloclength = Nlen;
PDATA-> NREFS = 1;
PDATA-> DATA () [NLEN] = '/ 0';
PDATA-> NDATALENGTH = NLEN;
M_PCHDATA = PDATA-> DATA ();
MFC Allocate Memory As Byte Array, Include CStringData and Data Buffer, And Convert It To CStringData Pointer, Nlen is The Length of String, And One More Byte, Store A '/ 0'.
In Debug Mode, When a Nlen <= 64, System Allocates 64 Bytes, When a Nlen <= 128, System Allocates 128 bytes ....
delete [] (byte *) pdata;
To Dispose Buffer, Because There CStringData Part (Header) And Data Buffer Part, MFC Convert the cstringdata * to byte Array, The delete it.
5. cstring operator =.
There are two methods to set one CString's value. If the source string is not a CString object, then MFC will allocate a new buffer to destination CString object, and use memcpy function to copy the content of source string to destination CString.m_pchData.
AllocbeforeWrite (NSRCLEN);
Memcpy (M_PCHDATA, LPSZSRCDATA, NSRCLEN * SIZEOF (TCHAR));
Getdata () -> ndatalength = nsrclen;
M_PCHDATA [NSRCLEN] = '/ 0';
In addition, if the source string is CString Object, commonly, the destination CString.m_pchData point to source CString.m_pchData. That means, the destination CString.m_pchData and the source CSting.m_pchData will point to the same address, destination CString and the Source Cstring Has The Same CstringData Object.when 2 Objects Share One CStringData, The Nref Member Variable of CstringData Will Be set to 2.
IF (M_PCHDATA! = Stringsrc.m_pchdata)
{
IF ((GetData ()-> NREFS <0 && getData ()! = _afxDatanil) ||
Stringsrc.getdata () -> NREFS <0)
{
// Actual Copy Necessary Since ONE OF THE STRINGS IS LOCKED
Assigncopy (Stringsrc.getdata () -> nDatalength, stringsrc.m_pchdata);
}
Else
{
// can Just Copy References AROUND
Release ();
Assert (stringsrc.getdata ()! = _Afxdatanil);
m_pchdata = stringsrc.m_pchdata;
InterlockedIncrement (& getData () -> NREFS);
}
}
Two CString share one CStringData will save system memory resource, but there is a risk, that when data in CString object's CStringData is changed, the other CString object will be changed at the same time, that may be is not we want.
There is a function in mfc named CopyBeforeWrite To DEAL with this qu.
Void cstring :: CopybeforeWrite ()
{
IF (getData () -> NREFS> 1)
{
CStringData * pdata = getdata ();
Release ();
Allocbuffer (PDATA-> NDATALENGTH);
Memcpy (M_PCHDATA, PDATA-> DATA (), (PDATA-> NDATALENGTH 1) * SIZEOF (TCHAR));
}
ASSERT (GetData () -> NREFS <= 1);
}
When one CString's value will be set, MFC will check if there is any other CString share the same CStringData object with it. If the reference bigger than 1, that means at least 2 CString object reference this CStringData object. The current, will be changed CString Must Create a New CstringData Object To Store The New Value.in The Release () Function, IF The Reference Bigger Than 1, The Buffer Will NOT Delete actual.
6. LockBuffer () and unlockbuffer ()
LockBuffer Will Set Nref Member To -1, And UNLockBuffer Will Set NREF MEMBER TO 1.
MSDN SAID: While In a Locked State, The String Is Protected in Two Ways:
No Other String Can Get a Reference To The Data In The Locked String, Even IF That String is assigned to the locked string.
The Locked String Will Never Reference Another String, Even if That Other string is copied to the locked string.
CSTRING STR1, STR2;
Str2 = "abcde";
Str2.lockbuffer ();
STR1 = STR2;
CString str3;
STR3 = "kk";
Str3.lockbuffer ();
STR3 = STR1;
The str2's buffer is locked, WHEN SET str1 Equal to str2, a new buffer will create to store the str2's value. Str1 and str2 have Different CstringsData Object.
If the str3's buffer is not locked, the str3 will share CStringData object with str1, but in this code, str3's CStringData object is locked, MFC will copy the value of str1 to str3, the str3's buffer address is not changed.
Of course, when CString is initializion without init-value, MFC will set a system defined, empty CStringData object to it, the nRef is -1, so LockBuffer () and UnLockBuffer () is no use to system defined, empty CStringData object.