Re: GTK signals question.



On Wed, 05 Mar 2014 13:43:29 -0500
Chris Moller <moller mollerware com> wrote:

On 03/05/14 12:07, Chris Vine wrote:
On Wed, 05 Mar 2014 09:44:48 -0500
Chris Moller <moller mollerware com> wrote:
I was actually writing that testcase when I found a correlation:
I'm using gcc and my callbacks were nested functions.  Pull the
callbacks out and make them normal, top-level, functions, and it
all works even without no blocking of any kind.  So, if this is a
bug at all, I suppose it could be a compiler bug.
Your question contained references to GTK+'s C interface.  If that
means that you are using C or C++ for your program, those languages
do not have a syntax for nested functions, so what you say does not
really make sense.  If you are simulating them in C++ using static
methods of a nested struct, in C++98 you have undefined behaviour
if you try to use them as a callback, because static member
functions of classes with local scope have no linkage (this follows
because §3.5/5 of the standard provides that a member function of
class scope has external linkage if the name of the class has
external linkage, which it doesn't in the case of local nested
structs, and §3.5/8 provides that "Names not covered by these rules
have no linkage").

So it is most likely a misunderstanding on your part rather than a
compiler bug, but you would need to post your code to be sure.

gcc supports nested functions as an extension to standard C.  I tend
to use them a lot because they operate within the stack frame of the 
enclosing function, thereby minimising the amount of information you 
have to pass.  This is especially valuable in GTK callbacks where you 
can only pass one pointer/int.

Having read Columban's posting, I see that the gcc documentation on
this extension explicitly rules out the use of nested functions as
pseudo-closures:  "If you try to call the nested function through its
address after the containing function exits, all hell breaks loose. If
you try to call it after a containing scope level exits, and if it
refers to some of the variables that are no longer in scope, you may be
lucky, but it's not wise to take the risk. If, however, the nested
function does not refer to anything that has gone out of scope, you
should be safe."

This is equivalent to the position with nested structs: they have no
linkage.  Presumably that was your problem in this case.

Chris


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