#If DEBUG is ignored (VB.net or C#) - c#

I have several of these in my code which have been working fine so far:
#If DEBUG Then
... some code here
#End If
Now, i am noticing that, lately, the code inside the " #If DEBUG Then ... #End If"
gets execute also in "Release Mode".
This is strange and did not happen before. What could have happened so that
the #If DEBUG are now being ignored (they are ignored both in debug in the IDE or the final executable) ?
I have applied Clean, Rebuild, etc.: no luck. Thank you for any hints and help.
-Pam

Firstly, make sure you understand the difference between how you're running the code and how you're building it. Too many people equate "launching in a debugger" with "the debug version" and "launching not in a debugger" with "the release version". They're completely orthogonal - you can launch a release build in a debugger (typically with less information available) and you can launch a debug build not in a debugger. Apologies if you were already aware of this.
Now, assuming you really have changed the project configuration you're building to Release, you need to check the project properties for that specific configuration. I don't know what it looks like in VB, but in C# in the project properties, in the build tab, there will be a list of defined symbols - that is what affects whether #if DEBUG code is built or not. Perhaps someone has copied over the project configuration from Debug into Release?
EDIT: One way to check this at build time is:
#if DEBUG
#error This shouldn't happen
#endif
In a release build, that should build without error. In debug, it won't.
EDIT: Another option is that your overall solution configuration is now referring to the wrong project configuration types. I can't remember the exact menu name, but if you look around Project for Configuration Manager, you should be able to bring up a grid mapping "Project" and "Solution Configuration" to the project configuration to build.

C# Project ( Visual Studio )
go to: Project Properties -> Build(tab)
Select Configuration: Release
Uncheck "Define DEBUG constant"
Now select Configuration: Debug
Check "Define DEBUG constant"
In your code, you can now type the following ( DEBUG with uppercase )
#IF DEBUG
// Debugging code goes here
#ENDIF

Under Project Properties / Compile / Advanced Compile Options there is
a checkbox called "Define Debug Constant" that sets this.
Check out: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24658238.html
EDIT: Try this initializing with this:
#If CONFIG = "Debug" Then
#CONST DEBUG = true
#if CONFIG = "Release" Then
#CONST DEBUG = false

Did you, by any chance, tick the "Define DEBUG constant" for the Release configuration, while you were in the Project Properties / Build?
Also make sure you are not building the project-level Debug configuration within the solution-level Release configuration (see the Configuration Manager).

Also remember #if DEBUG must be in uppercase. e.g. #if debug won't work.

Had a similar problem where "DEBUG" was never true. Tried by doing an uncheck and check of the "Define DEBUG constant" checkbox and rebuilding everytime but that did not work.
My solution was to define "DEBUG" manually in the "Conditional compilation symbols" textbox for the Debug configuration. When rebuilding, Visual Studio 2019 automatically removed the DEBUG symbol from the textbox (because this indeed should not be there) and from then on it worked again. When i switched from Debug to Release the correct lines got greyed out.
This seems to be a possible bug in VS 2019 (16.4.5)?

undefine DEBUG and that will not execute that portion.

If you are using ASP.NET make sure about this line in Web.Config file:
<compilation debug="false" targetFramework="4.5">
So if debug="true" your project runs in DEBUG mode.

If you are using VB.NETFramework v4.5 then use like
If Debugger.IsAttached Then
'... some code here
End If

Related

#if DEBUG and Conditional(“DEBUG”) called in Release build

I have a method that sets up some Debug only configurations, I have used this pattern on a few projects and with some of them it seems that nor #if DEBUG nor Conditional("DEBUG") are omitted in my release build.
Any ideas why?
Define DEBUG constant is checked:
As stated by #Evk, ""Define DEBUG constant" is checked. You need to uncheck it".
That defines DEBUG for the preprocessor even if it is not in the text box above it
Something that may be obvious for some: the Build configuration changes based on the selected Configuration (eg. Debug) and Platform (eg. Any CPU).
In order to have #if DEBUG and Conditional("DEBUG") working as expected, the DEBUG constant must be defined in the 'Debug' configuration only!
Notice the difference:
Debug:
Release:
If DEBUG is defined in Release, when using #if DEBUG or Conditional("DEBUG"), the condition will return true because the DEBUG constant exists in the project configuration.

How to identify the compilation mode of Cross Platform assemblies

How to detect the compilation mode (debug/release/release-xml) of cross platform assemblies (XForms.Android / XForms.iOS/ XForms.UWP) by programatically?
I have tried with refer below links.
https://www.hanselman.com/blog/HowToProgrammaticallyDetectIfAnAssemblyIsCompiledInDebugOrReleaseMode.aspx
This was working for all the assemblies except XForms.Android / XForms.iOS/ XForms.UWP assemblies.
Could you please provide me a solution for this?
You can do the following to execute code for debug or release:
#if DEBUG
// execute DEBUG code
#else
// execute release code
#endif
Note that you need to make sure that the "Define DEBUG constant" is checked. Not sure how it is for Xamarin studio, but in Visual Studio you can find it in the project properties in the "build" tab:
You can find some further information about #if in this SO question.

Visual Studio 2015: I can't debug and use breakpoints in Release mode

I have developed an UWP app that uses a lot of NuGet packages (MvvmLight, SQLite, ...) and other resources (Syncfusion controls).
I encounter a bug with the Store app, which is already published for tests, that is not present when I build the app in "Debug" mode.
So, I've tried to debug in "Release" mode, with the checked options "Compile with .NET Native tool chain" and "Optimize code". The build ends successfully, but I encounter an exception with a Syncfusion control (SfDataGrid) on the main page of the app. I would like use breakpoints to understand what happens, but they are deactivated as I build the app in "Release" mode.
If I build the app in "Debug" mode, with the same options checked ("Compile with .NET Native tool chain" and "Optimize code"), I don't encounter the same bug with the Syncfusion control, and the defined breakpoints are well keeped.
So I don't see how I could fix my problem:
if I create a new solution and built it in "Release" mode, the breakpoints are well keeped, and I can debug the code
if I build Syncfusion samples in "Release" mode, it's the same thing: the breakpoints are well keeped, and I can debug the code
I have compared the "build" parameters of the app and the other ones: they are the same
I have also looked at the "Just-in-Time" page, in the Visual Studio "options". I've got the following error: "Another debugger has registered itself as the Just-In-Time debugger. To repair, enable Just-In-Time debugging or run Visual Studio repair.". Ive tried to "repair" Visual Studio, but it's always the same thing...
Here is the "Options" settings:
And the result in solution, where breakpoints are disabled:
Would you have any explanation? How could I do to debug my app in "Release" mode?
[Edit 1]: add some details after further investigations
My app is based on a "template" like Template10, called Nentang. The structure of the project is the same, and they share a big part of references or NuGets packages.
But if I compare the build result of the "blank" Nentang and my solution, there are some differences that I don't understand:
as explained, on my app, the breakpoints and debug don't work in "Release" mode, and I can see that almost all modules don't have any "Symbol File":
=> only "ntdll.dll" and "KernelBase.dll" are linked to thier pdb file in a local directory: "C:\Users\myname\AppData\Local\Temp\SymbolCache"
on the "blank" Nentang app, the breakpoints and debug work well in "Release" mode, and I can see that almost all modules have a "Symbol File":
=> allmost half of the modules are linked to the same file in the "project" directory: "C:\Projects\Samples...\Nentang.UWP\bin\x64\Release\AppX\Nentang.UWP.pdb"
=> another quarter of the modules are linked to the same file of a "system" directory: "C:\Program Files (x86)\Microsoft SDKs\Windows Kits\10\ExtensionSDKs\Microsoft.NET.Native.Framework.1.3\1.3\x64\ret\Native\SharedLibrary.pdb"
=> the other modules are not linked to a Symbol File: it's the case of "ntdll.dll" and "KernelBase.dll"
How could I restore the "Symbol files" of my project?
I have also remarked a "strange" parameter in the properties of my solution, that is not present is the Nentang properties:
There is this parameter: "f:\dd\ndp\fxcore\CoreRT\src\System.Private.CoreLib\src\System\Runtime\ExceptionServices\ExceptionDispatchInfo.cs"
What does it mean? Could it explain my problem?
Debugging optimized code is always a challenge - even more so with .NETNative. Here are a few things you could try:
Disable Just My Code
Suppress JIT Optimizations: This will not help for modules built with .NETNative toolchain. If the exception occurs in Release builds without .NETNative, then check the debugger option called "Suppress JIT optimization on module load (Managed Only)". As the name implies, this will cause the CLR to JIT compile code unoptimized, which will allow you to set breakpoints and inspect locals.
Look at the Output window for clues as to what went wrong. The exception message will be there and the preceding messages may help diagnose the cause.
Debug your application with Native debug engine. You can do this by checking the Native checkbox under the Debug tab of the project properties.
Last resort is to debug the assembly.

#if DEBUG doesn't seem to work in my VS 2010

I've been doing the following in my C# code:
#if DEBUG
//Debugger only code
#endif
But for some reason when I do the same in my C# console application (that is built as a Windows service in VS 2010) it doesn't seem to work and for both Release and Debug configurations it looks like DEBUG is not defined.
Do I need to set something for that preprocessor constant to work?
When using the 'Debug' configuration, you have to go to project's Properties -> Build
and tick the 'Define DEBUG constant' box.
Either that, or define the conditional compilation symbol: DEBUG. As you can do with any other custom compilation symbol :)

c# Production code running DEBUG version

I have the following code:
#if (DEBUG)
imgPath = GetDirectoryName(Application.ExecutablePath);
#else
imgPath = GetDirectoryName(Application.ExecutablePath) + "\\images\\";
#endif
When the code went into Production (live site) , it still looked the the DEBUG version. How is this possible? Is there something during the promotion process that can indicate do RELEASE vs DEBUG
You might have deployed a version with binaries taken from a Debug build configuration or in any case, the DEBUG variable set to True.
You need to build in RELEASE and use that output as the release candidate.
When using #if statements, it is checking to determine if the constant is defined. Under the project's properties->Build section, the DEBUG constant is (by default) set to be defined when in debug mode. There is no RELEASE constant unless you define it yourself. You may have used your build from debug mode or had the "Define DEBUG constant" checked for your release build. Double check which you used and your build settings.
Also, your code should be greyed out to indicate that VS will not be compiling that code. So if the code you wish to be using is grey, that is an indicator you have DEBUG defined somewhere.

Categories

Resources