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

_DEBUG is not defined when compiling Godot in debug mode on Windows #31608

Closed
Zylann opened this issue Aug 23, 2019 · 1 comment · Fixed by #69294
Closed

_DEBUG is not defined when compiling Godot in debug mode on Windows #31608

Zylann opened this issue Aug 23, 2019 · 1 comment · Fixed by #69294

Comments

@Zylann
Copy link
Contributor

Zylann commented Aug 23, 2019

Godot 3.2 master

I am writing a module making use of STL because Godot containers are not fit for some use cases, and I realized that even in debug mode, they don't do any boundary checks. It would have been useful to have when I ran into heap corruption...

Then I realized it's because _DEBUG, a macro usually active on Windows debug builds to enable standard lib checks, was not defined. Is there a reason for that?

https://docs.microsoft.com/en-us/visualstudio/debugger/enabling-debug-features-in-visual-cpp-d-debug?view=vs-2019

I tried to enable it in my module, but then linking with the rest of Godot fails.

if env['target'] == 'debug':
	if env.msvc:
		env_mymodule.Append(CXXFLAGS=['/D_DEBUG'])
modules.windows.tools.64.lib(register_types.windows.tools.64.obj):-1: error: LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in crash_handler_windows.windows.tools.64.obj

I verified with this

#ifdef _DEBUG
	print_line("HELLO DEBUG");
#endif

EDIT: this can be fixed by modifying platform/windows/detect.py:

@@ -338,11 +338,11 @@ def configure_msvc(env, vcvars_msvc_config):
         env.AppendUnique(CPPDEFINES=["WINDOWS_SUBSYSTEM_CONSOLE"])
 
     ## Compile/link flags
 
     if env["use_static_cpp"]:
-        env.AppendUnique(CCFLAGS=["/MT"])
+        env.AppendUnique(CCFLAGS=["/MTd"])
     else:
         env.AppendUnique(CCFLAGS=["/MD"])

(of course we need to do this only in dev builds)
/MTd both defines _DEBUG and uses the debug runtime library, as _DEBUG alone won't work. Unfortunately, due to a bug in the debug runtime, any thread_local defined in global space holding a standard library container will cause an error when a debugger is attached.
See:
baldurk/renderdoc#1743
https://developercommunity.visualstudio.com/t/race-condition-on-g-tss-mutex-with-static-crt/672664
An easy workaround is to wrap these thread_local in a function, which will make them created on-demand instead of application start.
The only place I had to fix in Godot is the thirdparty oidn, which defines a thread_local in oidn/core/device.cpp.

@akien-mga
Copy link
Member

akien-mga commented Nov 28, 2022

Feel free to make a PR, including a patch for oidn if it's still needed.

This should be dependent on the optimize flag like godotengine/godot-cpp#932, e.g.:

diff --git a/platform/windows/detect.py b/platform/windows/detect.py
index 705e83dace..c9030fc982 100644
--- a/platform/windows/detect.py
+++ b/platform/windows/detect.py
@@ -339,10 +339,11 @@ def configure_msvc(env, vcvars_msvc_config):
 
     ## Compile/link flags
 
+    runtime_suffix = "d" if env["optimize"] in ["debug", "none"] else ""
     if env["use_static_cpp"]:
-        env.AppendUnique(CCFLAGS=["/MT"])
+        env.AppendUnique(CCFLAGS=["/MT" + runtime_suffix])
     else:
-        env.AppendUnique(CCFLAGS=["/MD"])
+        env.AppendUnique(CCFLAGS=["/MD" + runtime_suffix])
 
     if env["arch"] == "x86_32":
         env["x86_libtheora_opt_vc"] = True

Or another option which may work around the /MTd bug:

diff --git a/platform/windows/detect.py b/platform/windows/detect.py
index 705e83dace..2386ad159c 100644
--- a/platform/windows/detect.py
+++ b/platform/windows/detect.py
@@ -339,10 +339,14 @@ def configure_msvc(env, vcvars_msvc_config):
 
     ## Compile/link flags
 
-    if env["use_static_cpp"]:
-        env.AppendUnique(CCFLAGS=["/MT"])
+    if env["optimize"] in ["debug", "none"]:
+        # Always use dynamic runtime, debug static CRT is breaks thread_local.
+        env.AppendUnique(CCFLAGS=["/MDd"])
     else:
-        env.AppendUnique(CCFLAGS=["/MD"])
+        if env["use_static_cpp"]:
+            env.AppendUnique(CCFLAGS=["/MT"])
+        else:
+            env.AppendUnique(CCFLAGS=["/MD"])
 
     if env["arch"] == "x86_32":
         env["x86_libtheora_opt_vc"] = True

I guess I'll PR that for testing.

@akien-mga akien-mga added this to the 4.0 milestone Dec 5, 2022
rohanrhu pushed a commit to rohanrhu/godot that referenced this issue Dec 28, 2022
rohanrhu pushed a commit to rohanrhu/godot that referenced this issue Dec 28, 2022
Streq pushed a commit to Streq/godot that referenced this issue Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants