abseil / abseil/abseil-cpp

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

Abierto
#1,731 0 comentarios 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
C++
Estrellas
18.1k
Forks
3.2k
Merge medio
20 h 36 min
PR fusionados (30 d)
1

Descripción

### 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

#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
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

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.