Re: C++ virtual function compiler issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



If proper C++ runtime initialization does not occur, such as the
initialization of the virtual function table, then when the pointer to the
virtual function is executed, an access violation occurs because the pointer
to the virtual function was not initialized to the proper function in the
derived class. That would explain what's going on.

DarthDev
--
Let it be written. Let it be done.


"Paul G. Tobey [eMVP]" wrote:

> Yes, what does that have to do with whether the call itself or processing of
> the parameters or code inside the function is generating the error?
>
> Paul T.
>
> "DarthDev" <DarthDev@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:BB6B05EF-04E5-4FDE-8169-3B4180248C4F@xxxxxxxxxxxxxxxx
> > Did you read what Steve Maillet replied to the post? I am writing a
> > Service
> > DLL exposing the proper streams interface functions.
> >
> > Thanks,
> >
> > DarthDev
> > --
> > Let it be written. Let it be done.
> >
> >
> > "Paul G. Tobey [eMVP]" wrote:
> >
> >> That's what I'm wondering. It's the *call* that's generating the error
> >> (watch it execute in the debugger in disassembly mode), or the processing
> >> of
> >> the code in the call, or processing of parameters or return values?
> >>
> >> Paul T.
> >>
> >> "DarthDev" <DarthDev@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> >> news:75170C16-3842-475B-A792-DAD5DE1D86E2@xxxxxxxxxxxxxxxx
> >> > Sorry about that. When the virtual function ProcessUserRequest is
> >> > called,
> >> > is
> >> > when the acces violation exception is genereated with in the kernel
> >> > debugger.
> >> > My hardware is the Arcom Viper board equipped with an Intel Xscale
> >> > PA255
> >> > processor.
> >> >
> >> > Thanks,
> >> >
> >> > DarthDev
> >> >
> >> > --
> >> > Let it be written. Let it be done.
> >> >
> >> >
> >> > "Paul G. Tobey [eMVP]" wrote:
> >> >
> >> >> A little more detail, please. "When it's called"? Are you saying
> >> >> that,
> >> >> in
> >> >> the kernel debugger, when you attempt to step into a call something
> >> >> like
> >> >> this:
> >> >>
> >> >> LogService ls = new LogService();
> >> >> ls->Write();
> >> >>
> >> >> there's an immediate exception?
> >> >>
> >> >> What processor would also be important information. Is inlining
> >> >> enabled?
> >> >>
> >> >> Paul T.
> >> >>
> >> >> "DarthDev" <DarthDev@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> >> >> news:C2603B68-C523-48AC-BDF3-DA5D6E34712E@xxxxxxxxxxxxxxxx
> >> >> >I was coding up a service in C++ that is compiled in Platform Builder
> >> >> >when
> >> >> >I
> >> >> > came a cross this compiler issue. Here is a code example.
> >> >> >
> >> >> > class Service
> >> >> > {
> >> >> > public:
> >> >> > void Write();
> >> >> > virtual bool ProcessUserRequest(
> >> >> > unsigned char * UserMessage,
> >> >> > unsigned int UserMessageSize)=0;
> >> >> > };
> >> >> >
> >> >> > Service::Write()
> >> >> > {
> >> >> > unsigned char Buffer[256];
> >> >> >
> >> >> > bool Result=ProcessUserRequest(BufferSize,256);
> >> >> > }
> >> >> >
> >> >> > class LogService : public Service
> >> >> > {
> >> >> > virtual bool ProcessUserRequest(
> >> >> > unsigned char * UserMessage,
> >> >> > unsigned int UserMessageSize);
> >> >> > }
> >> >> >
> >> >> > bool LogService::ProcessUserRequest(
> >> >> > unsigned char * UserMessage,
> >> >> > unsigned int UserMessageSize)
> >> >> > {
> >> >> > int x;
> >> >> > x=1;
> >> >> > return(true);
> >> >> > }
> >> >> >
> >> >> > When the LogService class is instantiated in a service stream
> >> >> > interface
> >> >> > DLL
> >> >> > and Write() method is called, an access violation exception is
> >> >> > generated.
> >> >> > To
> >> >> > my knowledge this is good C++ code. I have compiled this code in
> >> >> > Embedded
> >> >> > Visual C++ and it executes fine. I am wondering if the compiler
> >> >> > goofing
> >> >> > up
> >> >> > the virtual function table.
> >> >> >
> >> >> > Thanks,
> >> >> >
> >> >> > DarthDev
> >> >> >
> >> >> > --
> >> >> > Let it be written. Let it be done.
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>
.



Relevant Pages

  • Initialization of virtual function table pointer
    ... examining the assembly code associated with constructors I noticed that the ... dynamic-dispatch mechanism was enabled before member initialization but after ... indirectly calling a virtual function. ...
    (comp.lang.cpp)
  • Re: Pure Virtual Function Calls
    ... If the this pointer is invalid. ... the correct function will start to execute but the function may crash when data via the this pointer is accessed. ... A virtual function uses a vtable, so when calling a virtual function the this pointer must be correct because the code that executes a virtual function will fetch the address for the function at the proper index of the vtable. ...
    (microsoft.public.dotnet.languages.vc)