Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-module exception issue with pre-compiler define _HAS_EXCEPTIONS=0 used in an application #4668

Closed
siren186 opened this issue Sep 9, 2024 · 3 comments · Fixed by #4725

Comments

@siren186
Copy link
Member

siren186 commented Sep 9, 2024

Hi @obiltschnig

I encountered a very subtle issue. It is about Poco::Exception in PocoFoundation64.dll. In Visual Studio 2019/2022 release_shared x64.
It is very hard to reproduce. Due to the large amount of code, I can't paste it all here, so I’ve only extracted a small portion.

// This is a small portion I extracted from a large amount of code.
int main() {
    std::uint64_t val = 0x1122334455667788;
    
    try {
    	throw Poco::Exception("error"); 
        // If I do not use Poco::XXXXXXException, everything works fine.
        // throw std::exception("error"); // It's OK !!!
    } catch (const Poco::Exception& e) {
    }
    
    std::cout << val; // Now the val is 0x1122334400000000 !!!
    // It's Very Strange !!! It lost low 32 bit.
    return 0;
}

It took me two days to find the real reason.

auto m = sizeof(Poco::Exception);
auto n = sizeof(std::exception);

// m=72, n=24: In PocoFoundation64.dll
// m=64, n=16: In my EXE

It was all caused by _HAS_EXCEPTIONS=0 in my EXE compile options. This macro will change the class size and implementation of std::exception.

// By: _HAS_EXCEPTIONS=1 (default in visual studio)
// sizeof(std::exception) == 24
namespace std { 
    class exception {
    public:
        virtual ~exception() {}
    private:
        char const* _What;
        bool        _DoFree;
    }
}


// By: _HAS_EXCEPTIONS=0
// sizeof(std::exception) == 16
namespace std { 
    class exception {
    public:
        virtual ~exception() {}
    protected:
        const char* _Ptr;
    }
}

I think it's necessary to add some tips in POCO documentation to avoid users wasting unnecessary time on this issue.


See alse: nodejs/node-addon-api#85

@aleks-f
Copy link
Member

aleks-f commented Oct 3, 2024

This is cmake build system problem. VS projects have /EHsc

@aleks-f aleks-f added this to 1.14 Oct 3, 2024
@aleks-f aleks-f added this to the Release 1.14.0 milestone Oct 3, 2024
@matejk matejk moved this to In Progress in 1.14 Oct 4, 2024
@matejk
Copy link
Contributor

matejk commented Oct 4, 2024

This is cmake build system problem. VS projects have /EHsc

CMake adds /EHsc by default when building on Windows:

C:\PROGRA~2\MICROS~2\2019\ENTERP~1\VC\Tools\MSVC\1429~1.301\bin\HostX64\x86\cl.exe /nologo /TP -DFoundation_EXPORTS -DPOCO_ENABLE_CPP11 -DPOCO_ENABLE_CPP14 -DPOCO_OS_FAMILY_WINDOWS -DUNICODE -DUTF8PROC_STATIC -D_DEBUG -D_UNICODE -IC:\github\build-poco-Desktop_Qt_5_15_2_MSVC2019_32bit-Debug\Foundation -IC:\github\poco\Foundation\include -IC:\github\poco\Foundation\src /DWIN32 /D_WINDOWS /GR /EHsc /Zc:__cplusplus /Zi /Ob0 /Od /RTC1 -std:c++17 -MDd /showIncludes /FoFoundation\CMakeFiles\Foundation.dir\src\ByteOrder.cpp.obj /FdFoundation\CMakeFiles\Foundation.dir\ /FS -c C:\github\poco\Foundation\src\ByteOrder.cpp
The problem is caused by defining _HAS_EXCEPTIONS=0 in the application that is using Poco. The size of std::exception and Poco::Exception inside Poco binaries is correct.

Additional define in the application is the root of the problems, not the way Poco is built.

@matejk matejk changed the title Cross-module exception handling Cross-module exception issue with pre-compiler define _HAS_EXCEPTIONS=0 used in an application Oct 4, 2024
@matejk
Copy link
Contributor

matejk commented Oct 4, 2024

What can be done in Poco is to verify the value of _HAS_EXCEPTIONS in Poco header files and abort compile if it is set to 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants