[C++] Virtual methods

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

[C++] Virtual methods

Post by AlfaOmega08 »

I have recently improved my C++ knowledge, learning how to use virtual methods.
But when calling a virtual method, my kernel does a Page Fault with CR2 = 0x0
Also calling the inherited method I have a Page Fault. Removing the virtual keyword, everything works fine, except the fact that I cannot use the overwriting of methods in inherited classes.
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
neonek
Member
Member
Posts: 38
Joined: Thu Aug 28, 2008 1:53 pm
Location: Białystok - Podlasie, Poland

Re: [C++] Virtual methods

Post by neonek »

Maybe our wiki entry will help you click.
Please correct my English. If you'll find mistake please tell me about it so I can improve my English.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Re: [C++] Virtual methods

Post by AlfaOmega08 »

Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:

Code: Select all

src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
Please, correct my English...
Motherboard: ASUS Rampage II Extreme
CPU: Core i7 950 @ 3.06 GHz OC at 3.6 GHz
RAM: 4 GB 1600 MHz DDR3
Video: nVidia GeForce 210 GTS... it sucks...
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: [C++] Virtual methods

Post by skyking »

AlfaOmega08 wrote:Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:

Code: Select all

src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
No, the wiki says virtual functions should work even with RTTI disabled. Have you run the constructor properly for the object? Or more precisely does the vtable pointer point to the vtable?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: [C++] Virtual methods

Post by JamesM »

skyking wrote:
AlfaOmega08 wrote:Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:

Code: Select all

src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
No, the wiki says virtual functions should work even with RTTI disabled. Have you run the constructor properly for the object? Or more precisely does the vtable pointer point to the vtable?
And is the this pointer correct... post the code.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: [C++] Virtual methods

Post by xenos »

A CR2 value of 0x0 means you are trying to access memory at virtual address 0x0. If this happens when you call a virtual function, that probably means that the function pointer in the VTable is illegal (it points to 0). I think this happens if the constructor is not called properly.

BTW, using -fno-rtti and virtual functions works, since VTable and RTTI are two different things.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: [C++] Virtual methods

Post by Creature »

Note that you need a few supporting functions (like it says on the wiki), like for when a pure virtual call cannot be made (I believe you have to define __cxa_pure_virtual or something equivalent [check the wiki] and make it plainly return 0). There's also some other support code needed.

As far as I know, virtual functions can work perfectly without RTTI (what else would I've been using in my OS?), I haven't tried pure virtual functions properly yet, though.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: [C++] Virtual methods

Post by skyking »

[quote="Creature"]Note that you need a few supporting functions (like it says on the wiki), like for when a pure virtual call cannot be made (I believe you have to define __cxa_pure_virtual or something equivalent [check the wiki] and make it plainly return 0). There's also some other support code needed.[/qoute]

Yes check the wiki. No you don't need any supporting functions for just ordinary virtual functions, it's only pure virtual that it is needed for, and no __cxa_pure_virtual is not supposed to be called - it's an error and you'd be better of if you don't return 0 (you'd be better of if you do something completely different like generating a trap screen or something).

I guess that the original poster did not use pure virtual functions and did not force the link to succeed despite unresolved symbols.
Post Reply