[almost solved] Null device OSOperator crash

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kormoran
Posts: 47
Joined: Mon Dec 28, 2015 4:50 pm
Location: Tolentino

[almost solved] Null device OSOperator crash

Post by kormoran »

I'm using EDT_NULL to probe some system values before starting the real thing, but OSOperator crashes the program at first use. Here's the code:

Code: Select all

    u32 *TotMem;
    u32 *AvailMem;
    u32 *MHZ;
    IVideoModeList *Modelist;
    IrrlichtDevice* probe = createDevice(EDT_NULL);
    Modelist = probe->getVideoModeList();
    IOSOperator *op = probe->getOSOperator();
    op->getSystemMemory(TotMem, AvailMem);    // Crash!
    op->getProcessorSpeedMHz(MHZ);
    probe->closeDevice();
 
I tested the op pointer: it's not null. Why is it crashing, then?
The program crashes with return code 0xC0000005, if this can be useful.

(EDIT) Just seen in the sources, the null device does NOT return a valid OSOperator pointer :evil: :evil: :evil:
Last edited by kormoran on Sun Feb 07, 2016 7:04 pm, edited 2 times in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [solved] Null device OSOperator crash

Post by CuteAlien »

Hm, that that operator should be set by the devices, shouldn't matter which driver is used. Can you make a quick complete program which I can compile that reproduced the problem? And please tell me on which OS you are (Linux, Windows, Apple, Mobile phones etc...).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
kormoran
Posts: 47
Joined: Mon Dec 28, 2015 4:50 pm
Location: Tolentino

Re: [solved] Null device OSOperator crash

Post by kormoran »

CuteAlien wrote:Hm, that that operator should be set by the devices, shouldn't matter which driver is used.
I tried this morning with the device I'm using (EDT_OPENGL) and program crashed the same way.

A quick peek with the debugger showed me that the point of failure was inside Irrlicht code:

Code: Select all

 
bool COSOperator::getSystemMemory(u32* Total, u32* Avail) const
{
#if defined(_IRR_WINDOWS_API_) && !defined (_IRR_XBOX_PLATFORM_)
    MEMORYSTATUS MemoryStatus;
    MemoryStatus.dwLength = sizeof(MEMORYSTATUS);
 
    // cannot fail
    GlobalMemoryStatus(&MemoryStatus);
 
    if (Total)
        *Total = (u32)(MemoryStatus.dwTotalPhys>>10); // Crash!
    if (Avail)
        *Avail = (u32)(MemoryStatus.dwAvailPhys>>10);
 
 
The file is COSOperator.cpp, line 179. It crashes exactly in the same point (tried both EDT_NULL and EDT_OPENGL: no differences)

You can try this code:

Code: Select all

#include <irrlicht.h>
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
 
int main(int argc, char** argv)
{
    u32 *TotMem;
    u32 *AvailMem;
    u32 *MHZ;
 
    IrrlichtDevice* probe = createDevice(EDT_NULL);
    IOSOperator *op = probe->getOSOperator();
    op->getSystemMemory(TotMem, AvailMem);
    op->getProcessorSpeedMHz(MHZ);
    probe->closeDevice();
}
 
I'm on Win7 x64 SP1, processor AMD Phenom II, ram 8GB, ATI Radeon 5770.
I'm using Code::blocks 16.01 and TDM MINGW64 5.2.0 to compile both Irrlicht and my app.

(EDIT) the exact error given is segmentation fault (SIGSEGV). Right now I tried to call GlobalMemoryStatus directly:

Code: Select all

    u32 *TotMem;
    u32 *AvailMem;
    MEMORYSTATUS MemoryStatus;
    MemoryStatus.dwLength = sizeof(MEMORYSTATUS);
    
    GlobalMemoryStatus(&MemoryStatus);
    *TotMem = (u32)(MemoryStatus.dwTotalPhys>>10);  // Crash! Same error
    *AvailMem = (u32)(MemoryStatus.dwAvailPhys>>10);
 
so I think it's not Irrlicht fault but something between MinGW and Windows...
kormoran
Posts: 47
Joined: Mon Dec 28, 2015 4:50 pm
Location: Tolentino

Re: [not so solved] Null device OSOperator crash

Post by kormoran »

Very interesting. THIS code works:

Code: Select all

    u64 TotMem;
    u64 AvailMem;
    u32 *MHZ;
    MEMORYSTATUSEX MemoryStatus;
    MemoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
 
    GlobalMemoryStatusEx(&MemoryStatus);
    TotMem = (u64)MemoryStatus.ullTotalPhys;
    AvailMem = (u64)MemoryStatus.ullAvailPhys;
    TotMem = TotMem>>10;
    AvailMem = AvailMem>>10;
 
From the Microsoft Windows Dev center pages:
On computers with more than 4 GB of memory, the GlobalMemoryStatus function can return incorrect information, reporting a value of –1 to indicate an overflow. For this reason, applications should use the GlobalMemoryStatusEx function instead. (blah blah...)
Having 8GB, I switched to the "Ex" version, and probably the OSOperator code too needs that, since modern PCs have very often more than 4GB (and the "blah blah" above tells of troubles even with >2GB). Anyway, however necessary this update was, I suspect it's not the reason of the fix... hmmm... investigation goes on

(EDIT) It's the pointer.

Code: Select all

 
    u64 *TotMem;
    // etc. etc...
    *TotMem = (u64)(MemoryStatus.ullTotalPhys>>10); // Crash! Segmentation fault
 
Instead:

Code: Select all

 
    u64 TotMem;
    // etc. etc...    
    TotMem = (u64)(MemoryStatus.ullTotalPhys>>10); // It works perfectly 
 
I didn't tried with the old "no-Ex" version, but shouldn't be necessary. We got the criminal, but WHY a honest user-side pointer should wreak so much havoc? :shock:
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: [not so solved] Null device OSOperator crash

Post by CuteAlien »

Ah right. getSystemMemory works with pointers. So should be u32 TotMem; and then you pass &TotMem to get it written in there. Otherwise you basically wrote in the whatever place that pointer was having as value (aka undefined).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
kormoran
Posts: 47
Joined: Mon Dec 28, 2015 4:50 pm
Location: Tolentino

Re: [almost solved] Null device OSOperator crash

Post by kormoran »

OK, using non-pointer u32 and references in the function call solves the error... but memory reports returned from OSOperator are still not correct.
I'm patching my copy of Irrlicht to use GlobalMemoryStatusEx, will post it on Irrlicht bugtracker when done... :!:
Post Reply