[webkit-efl] FW: Use of pimpl idiom for Ewk classes
Dumez, Christophe
christophe.dumez at intel.com
Fri Oct 19 04:21:49 PDT 2012
>
> > You can already see it in my patch for the destination() getter. It has
> to return a "const char*" instead of a String because it is used by the C
> API.
> IMHO, external object(s) should treat Ewk classes as C++ as much as
> possible in this case.****
>
> Therefore, we don’t have to make functions that return char*.****
>
> Rather, caller can use utilization function to change String class to
> char*.****
>
> ** **
>
> --- Ewk class****
>
> String destination() { … }****
>
> --- Utilization function****
>
> char* StoCP(String s)****
>
> {****
>
> char* cp = s.utf8().data();****
>
> return cp;****
>
> }****
>
> --- C layer****
>
> void i_am_c_public_api()****
>
> {****
>
> char* cp = StoCP(destination);****
>
> }
>
Eh what? This is wrong and will crash.
char* StoCP(String s) {
char* cp = s.utf8().data(); // s.utf8() returns a temporary CString
object and you store a pointer to the internal representation of that
temporary CString object
return cp; // You return a pointer to an area in memory that is no
longer valid because it was temporary.
}
Secondly, even if that worked, we loose all benefit of Eina Stringsharing
since we would return raw char* to the client.
Kr,
--
Christophe Dumez
Linux Software Engineer, PhD
Intel Finland Oy - Open Source Technology Center
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-efl/attachments/20121019/7211b940/attachment.html>
More information about the webkit-efl
mailing list