[SDL] Problem with threads and virtual functions

Tyler Montbriand tsm at accesscomm.ca
Mon Nov 17 15:58:01 PST 2003


On Monday 17 November 2003 03:16 pm, Glenn Maynard wrote:
> You're creating the thread during the base class ctor.  In there, the
> object is still the base class.  The base class ctor can not access the
> derived class virtuals--they aren't there yet.
Thanks for the reply!  :)

I googled for a couple hours on this and tried that too - no luck.  :(  I 
should have left that code in, but I trimmed it out to make my code smaller..  
it's pretty big for posting as is.

I could understand that problem happening if I was trying to call derived 
class virtuals IN THE CONSTRUCTOR - but I'm not.  I'm just creating 
threads...  the pointer is not dereferenced by the thread until long after 
the constructor returns.  Indeed, the semaphore is there to guarantee that.

Hm.  Are virtual tables for stack variables created in stack space?  That 
could explain it - when the thread gets created, it would get a copy of the 
stack with no virtual table in it yet.  Except that still wouldn't explain 
why it doesn't work when I create the threads after the constructor returns..  
perhaps it's a bug in g++.  Anyway, I've changed it from a virtual function 
to a function pointer, that's working much better.

> (It's off-topic here; this behavior isn't SDL's fault. :)
I thought I might have overlooked something obvious about how threads work, or 
some peculiarity about SDL threads.  Oh well.




More information about the SDL mailing list