Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if I don't call ReleaseBuffer after GetBuffer?

From CString to char*, ReleaseBuffer() must be used after GetBuffer(). But why? What will happen if I don't use ReleaseBuffer() after GetBuffer()?

Can somebody show me an example? Thanks.

like image 687
Landy Avatar asked Feb 26 '10 15:02

Landy


2 Answers

I'm not sure that this will cause a memory leak, but you must call ReleaseBuffer to ensure that the private members of CString are updated. For example, ReleaseBuffer will update the length field of the CString by looking for the terminating null character.

like image 199
Nick Meyer Avatar answered Oct 26 '22 15:10

Nick Meyer


What will happen if I don't use ReleaseBuffer() after GetBuffer()?

I haven't used MFC (and hopefully won't ever have to touch it with a ten-foot pole) but, as a rule of thumb, whenever you have an API that has both GetXXX() and ReleaseXXX() (especially when the result of GetXXX() conveniently is of the type that ReleaseXXX() takes) -- then when you forget to call ReleaseXXX() for every one of your GetXXX() calls, you will leak an XXX.

like image 23
sbi Avatar answered Oct 26 '22 15:10

sbi