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.
[C++] Virtual methods
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
[C++] Virtual methods
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...
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...
Re: [C++] Virtual methods
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.
- AlfaOmega08
- Member
- Posts: 226
- Joined: Wed Nov 07, 2007 12:15 pm
- Location: Italy
Re: [C++] Virtual methods
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:
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
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'
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...
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...
Re: [C++] Virtual methods
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?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:
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?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'
Re: [C++] Virtual methods
And is the this pointer correct... post the code.skyking wrote: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?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:
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?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'
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: [C++] Virtual methods
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.
BTW, using -fno-rtti and virtual functions works, since VTable and RTTI are two different things.
Re: [C++] Virtual methods
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.
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.
Re: [C++] Virtual methods
[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.
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.