type-punning warnings with optimizations turned on?



I have the following snippit:

        #include <glib.h>
        
        struct MyMutex {
            GStaticMutex mutex;
        };
        
        MyMutex * MyMutex_new() {
            MyMutex *result = g_new(MyMutex,1);
            g_static_mutex_init(&result->mutex);
            return result;
        }
        
        void MyMutex_free(MyMutex *mutex) {
            g_static_mutex_free(&mutex->mutex);
            g_free(mutex);
        }
        
        void MyMutex_lock(MyMutex *mutex) {
            g_static_mutex_lock(&mutex->mutex);
        }
        
        void MyMutex_unlock(MyMutex *mutex) {
            g_static_mutex_unlock(&mutex->mutex);
        }

When compiled without optimizations, I get no warnings:

        g++ -O0 -Wall -c `pkg-config --cflags glib-2.0` test.cpp

But with optimizations on (actually, at or above -O2), I get warnings
about type-punning on the calls to g_static_mutex_lock/unlock:

        g++ -O2 -Wall -c `pkg-config --cflags glib-2.0` test.cpp
        test.cpp: In function âvoid MyMutex_lock(MyMutex*)â:
        test.cpp:19: warning: type-punning to incomplete type might break strict-aliasing rules
        test.cpp: In function âvoid MyMutex_unlock(MyMutex*)â:
        test.cpp:23: warning: type-punning to incomplete type might break strict-aliasing rules
        
Did I do something wrong syntactically? Or is the source of this error
in GLib or perhaps gcc? I use GStaticMutex all over my code; this is the
only place producing a warning.

For information, the development system is FC6.x86_64 with GLib 2.12.9,
gcc 4.1.2.

Thanks for any suggestions.

-Alan (big fan of warning-free builds)





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]