For TabCtrl_InsertItem, do we need keep pszText member points to valid memory or will the OS copy the text so that we can straight away clear the text?
TCITEM tcItem = { TCIF_TEXT };
CString strText;
for (int i=0; i<3; i++) {
strText.Format(_T("Tab %d"), i+1);
tcItem.pszText = strText; // option 1
tcItem.pszText = _tcsdup(strText); // option 2
TabCtrl_InsertItem(i, &tcItem);
}
How can we know which is the correct one for this function and other functions in future? I couldn't find it in MSDN.
The operating system will copy the string to an internal data structure. You do not need to maintain the backing character array. You can free it as soon as the function call is complete.
In general, this is the case for any Windows API function that receives a string. You're right, it doesn't say it explicitly in the MSDN documentation, but most Windows developers already know this. MSDN will call out explicitly when this is not true. I can't think of an example off the top of my head—if it ever occurs, it is quite rare.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With