3

This is a very strange problem here:

I have a solution that consists of two projects, one is C# and the other is a C++/CLR.

C++/CLR project loads a DLL and exposes its functionality as a managed DLL to be used by C#.

When the application finishes, the DLL should be deleted. I have a suitable code in my code to free the library.

The last part of this process is to actually free the library and I have this code (This is C++ code)

enter image description here

As you can see there is a breakpoint and also an std::cout line.

If I comment out the std::cout line, then the breakpoint is not called.

If I let the std::cout active in the code, the breakpoint hits and it prints out.

I am building in Debug mode so I don't believe the code removed as part of optimization. I cleaned the project and have the same issue. I rebuild the project and have the same issue.

I am wondering why this is happening and can I be sure that the library is freed in this case?

Edit 1

I think there is no code optimization (I am using MSVC 2019):

enter image description here

mans
  • 17,104
  • 45
  • 172
  • 321
  • 1
    what is your compelation flags are? it look like compailer optimization – yaodav Jun 24 '20 at 08:49
  • read the second answer in this [question](https://stackoverflow.com/questions/2219829/how-to-prevent-gcc-optimizing-some-statements-in-c) on how to prevent code optimization – yaodav Jun 24 '20 at 08:55
  • @yaodav Please see the updated question. I believe all optimizations are disabled. Is there anywhere else that I should look for optimization? – mans Jun 24 '20 at 08:59
  • or use pragma or use volatile var, but if the compiler is optimize your code be sure that he is also deleting your library – yaodav Jun 24 '20 at 11:05
  • @yaodav Can you please elaborate? How to use pragma? – mans Jun 24 '20 at 11:37
  • like [this](https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/Function-Specific-Option-Pragmas.html) `#pragma GCC optimize ("O0") your code` – yaodav Jun 24 '20 at 11:44
  • @yaodavThis is for GCC,but I am using Visual Studio for his project. – mans Jun 24 '20 at 14:00
  • try [this](https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-optimized-code?view=vs-2019) visual studio doc – yaodav Jun 24 '20 at 14:33
  • http://idownvotedbecau.se/imageofcode – Werner Henze Jun 26 '20 at 06:33
  • 1
    @WernerHenze I understand your point, In this case the actual code is not important, but the position of breakpoint and why it is not hit is important. If I copied the code, It doesn't give more information. I always copy and paste my code as I know that image of code is not sufficient for code debugging. – mans Jun 26 '20 at 15:17

0 Answers0