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

[Bug]: absl::Log Initialization in DLL Fails When Called Using ctypes on Windows #1731

Open
jiawlu opened this issue Jul 26, 2024 · 0 comments

Comments

@jiawlu
Copy link

jiawlu commented Jul 26, 2024

Describe the issue

I am developing a dynamic library using C++ and utilizing ctypes in Python to call functions from this library. When I try to initialize absl::Log within the DLL, I encounter the following error:

OSError: exception: access violation reading 0x0000000000000000
This issue only occurs on Windows with MSVC; the same code works fine on macOS with the Clang compiler.

Below is a demo code to illustrate the issue.

main.cpp

#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "absl/log/log.h"
#include <iostream>

#ifdef _WIN32
#define C_API __declspec(dllexport)
#else
#define C_API
#endif


extern "C" {

C_API void initializeAbslLoggingPy() {
  LOG(INFO) << "initializing absl log";
  absl::InitializeLog();
  absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
  LOG(INFO) << "absl log initialized";
}
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(osm2gmns VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)


include(FetchContent)
set(FETCHCONTENT_QUIET OFF)

SET(ABSL_PROPAGATE_CXX_STD ON)
SET(ABSL_BUILD_TESTING OFF)
FetchContent_Declare(
        absl
        GIT_REPOSITORY https://github.com/abseil/abseil-cpp.git
        GIT_TAG 20230125.3
        SYSTEM
)
FetchContent_MakeAvailable(absl)

add_library(main SHARED main.cpp)
target_link_libraries(main PRIVATE absl::log absl::log_initialize)

main.py

import ctypes
import os

oglib = ctypes.CDLL(os.path.join(os.path.dirname(__file__), 'main.dll'))
oglib.initializeAbslLoggingPy.argtypes = []
oglib.initializeAbslLoggingPy()

error message

WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1721963952.103176   15648 main.cpp:17] initializing absl log
Traceback (most recent call last):
  File "c:\Users\Jiawei\Desktop\og\absl\test\main.py", line 6, in <module>
    oglib.initializeAbslLoggingPy()
OSError: exception: access violation reading 0x0000000000000000

If I build an executable on Windows with the following code, it works fine.

#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
#include "absl/log/initialize.h"
#include "absl/log/log.h"
#include <iostream>

int main() {
  LOG(INFO) << "initializing absl log";
  absl::InitializeLog();
  absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
  LOG(INFO) << "absl log initialized";
}

Steps to reproduce the problem

cmake ..
cmake --build .
copy the built main.dll file to the same folder with main.py
python main.py

What version of Abseil are you using?

tried both GIT_TAG 20230125.3 and 20240116.2

What operating system and version are you using?

windows 10

What compiler and version are you using?

microsoft visual studio 2022

What build system are you using?

microsoft visual studio 2022

Additional context

the same code works fine on macOS with the Clang compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant