CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Memory leaks when mixing managed & native code

    In my current assignment there is an application where they have mixed managed (I have never worked with managed C++ before) and native C++ code. I tried to introduce the usage of smart pointers and as soon as I added a static one... BAM! The code didn't even make it to main before crashing. After some surfing I found out that the reason for that is that in managed C++ the crt startup code is bypassed.

    The solution given on the net was to add __DllMainCRTStartup@12 in linker Force Symbol References. Even though it feels a bit strange to call Dll... it seems to do the trick of getting all statics properly initialized. What I've failed to find though is how to force a call to the cleanup on exit. Has anybody here some insight?

    If anyone is curious I've attached a minimalistic 2010 Express project that demonstrate the issue. Comment/uncomment the define in NativeLib.cpp, run and then just Close the form to see the leak report (or not depending on the define).

    Edit: I forgot to mention that the project settings are copied from the full one so it might be something that look peculiar for the posted one.
    Attached Files Attached Files
    Last edited by S_M_A; February 11th, 2013 at 04:41 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured