Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabCtrl_InsertItem: do we need to keep memory of the text alive?

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.

like image 694
Afriza N. Arief Avatar asked Dec 18 '25 14:12

Afriza N. Arief


1 Answers

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.

like image 115
Cody Gray Avatar answered Dec 21 '25 06:12

Cody Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!