Re: [webkit-dev] deleteOwnedPtr(T* ptr)
Umm... shouldn't this behavior be commented so that people are not left wondering why it fails and trying to fix it? On Wed, Nov 10, 2010 at 16:04, Darin Adler da...@apple.com wrote: On Nov 10, 2010, at 2:33 PM, Daebarkee Jung wrote: I found that the following lines made errors: // OwnPtrCommon.h template typename T inline void deleteOwnedPtr(T* ptr) { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete ptr; } I am very curious about why the author wrote like the above. What could be the author's intention? The code is to prevent issues like the ones described on these websites: http://stackoverflow.com/questions/1767679/incomplete-type-memory-leaks http://bytes.com/topic/c/answers/611877-gcc-class-forward-declarations-destructor-calls http://connect.microsoft.com/VisualStudio/feedback/details/231177/delete-of-pointer-to-incomplete-class If we delete a pointer and the object has incomplete type, we get undefined behavior. Instead this code causes compilation to fail if the object has incomplete type. The use of a negative number for the size of an array is a way to guarantee we get a compilation error. Your alternate version might also work; I’m not sure. -- Darin ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
Re: [webkit-dev] deleteOwnedPtr(T* ptr)
Thanks, all of you. Now, everything is clear. Daebark On Thu, Nov 11, 2010 at 2:24 AM, Finnur Thorarinsson fin...@chromium.orgwrote: Umm... shouldn't this behavior be commented so that people are not left wondering why it fails and trying to fix it? On Wed, Nov 10, 2010 at 16:04, Darin Adler da...@apple.com wrote: On Nov 10, 2010, at 2:33 PM, Daebarkee Jung wrote: I found that the following lines made errors: // OwnPtrCommon.h template typename T inline void deleteOwnedPtr(T* ptr) { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete ptr; } I am very curious about why the author wrote like the above. What could be the author's intention? The code is to prevent issues like the ones described on these websites: http://stackoverflow.com/questions/1767679/incomplete-type-memory-leaks http://bytes.com/topic/c/answers/611877-gcc-class-forward-declarations-destructor-calls http://connect.microsoft.com/VisualStudio/feedback/details/231177/delete-of-pointer-to-incomplete-class If we delete a pointer and the object has incomplete type, we get undefined behavior. Instead this code causes compilation to fail if the object has incomplete type. The use of a negative number for the size of an array is a way to guarantee we get a compilation error. Your alternate version might also work; I’m not sure. -- Darin ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
Re: [webkit-dev] deleteOwnedPtr(T* ptr)
On Nov 11, 2010, at 12:24 AM, Finnur Thorarinsson wrote: Umm... shouldn't this behavior be commented so that people are not left wondering why it fails and trying to fix it? Sure, sounds good. Instructions for submitting patches to add things like those comments are at http://webkit.org/coding/contributing.html. -- Darin ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
Re: [webkit-dev] deleteOwnedPtr(T* ptr)
Do or do not. Then, there is no question. :) btw, in another code base, you may familiar with, the comment is the variable name: http://www.google.com/codesearch?q=type_must_be_completeexact_package=chromium On Thu, Nov 11, 2010 at 12:24 AM, Finnur Thorarinsson fin...@chromium.orgwrote: Umm... shouldn't this behavior be commented so that people are not left wondering why it fails and trying to fix it? On Wed, Nov 10, 2010 at 16:04, Darin Adler da...@apple.com wrote: On Nov 10, 2010, at 2:33 PM, Daebarkee Jung wrote: I found that the following lines made errors: // OwnPtrCommon.h template typename T inline void deleteOwnedPtr(T* ptr) { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete ptr; } I am very curious about why the author wrote like the above. What could be the author's intention? The code is to prevent issues like the ones described on these websites: http://stackoverflow.com/questions/1767679/incomplete-type-memory-leaks http://bytes.com/topic/c/answers/611877-gcc-class-forward-declarations-destructor-calls http://connect.microsoft.com/VisualStudio/feedback/details/231177/delete-of-pointer-to-incomplete-class If we delete a pointer and the object has incomplete type, we get undefined behavior. Instead this code causes compilation to fail if the object has incomplete type. The use of a negative number for the size of an array is a way to guarantee we get a compilation error. Your alternate version might also work; I’m not sure. -- Darin ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
Re: [webkit-dev] deleteOwnedPtr(T* ptr)
On Nov 11, 2010, at 7:20 AM, Darin Adler wrote: On Nov 11, 2010, at 12:24 AM, Finnur Thorarinsson wrote: Umm... shouldn't this behavior be commented so that people are not left wondering why it fails and trying to fix it? Sure, sounds good. Instructions for submitting patches to add things like those comments are at http://webkit.org/coding/contributing.html. Even better than a comment would be to use the COMPILE_ASSERT macro. Regards, Maciej ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
[webkit-dev] deleteOwnedPtr(T* ptr)
Hi, I found that the following lines made errors: // OwnPtrCommon.h template typename T inline void deleteOwnedPtr(T* ptr) { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete ptr; } I am very curious about why the author wrote like the above. What could be the author's intention? For error-fix, I changed it like the following: template typename T inline void deleteOwnedPtr(T* ptr) { if(sizeof(T)0) delete ptr; } What could be the above code's mistake? Thanks, Daebark ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
Re: [webkit-dev] deleteOwnedPtr(T* ptr)
On Nov 10, 2010, at 2:33 PM, Daebarkee Jung wrote: I found that the following lines made errors: // OwnPtrCommon.h template typename T inline void deleteOwnedPtr(T* ptr) { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete ptr; } I am very curious about why the author wrote like the above. What could be the author's intention? The code is to prevent issues like the ones described on these websites: http://stackoverflow.com/questions/1767679/incomplete-type-memory-leaks http://bytes.com/topic/c/answers/611877-gcc-class-forward-declarations-destructor-calls http://connect.microsoft.com/VisualStudio/feedback/details/231177/delete-of-pointer-to-incomplete-class If we delete a pointer and the object has incomplete type, we get undefined behavior. Instead this code causes compilation to fail if the object has incomplete type. The use of a negative number for the size of an array is a way to guarantee we get a compilation error. Your alternate version might also work; I’m not sure. -- Darin ___ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev