allocation/deallocation of memory in exe and library
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: allocation/deallocation of memory in exe and library

  1. #1
    Join Date
    Jan 2003
    Posts
    615

    allocation/deallocation of memory in exe and library

    Say I have an executable and a library on the Windows XP platform.

    Does building the executable and library as static (/MT) or dynamic (/MD) have any impact on which unit creates and deletes memory on the heap? The reason for asking is that I read somewhere that if data is allocated on the heap in a library it must also be deallocated in the same library (and same for exe). Just wanted to check if this is true regardless of building options.

    Cheers :-)
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: allocation/deallocation of memory in exe and library

    Quote Originally Posted by laasunde View Post
    Say I have an executable and a library on the Windows XP platform.

    Does building the executable and library as static (/MT) or dynamic (/MD) have any impact on which unit creates and deletes memory on the heap? The reason for asking is that I read somewhere that if data is allocated on the heap in a library it must also be deallocated in the same library (and same for exe). Just wanted to check if this is true regardless of building options.
    When you link to a static library in an executable, it is as if all the source files were included in your executable. So, it's safe to allocate something in a static library and deallocate it in the executable that links to that library.
    With a DLL it is possible that the DLL is build against a different version of the C runtime library (CRT) than the executable. In that case, it's not safe to allocate something in the DLL and deallocate it in the executable. Only if both are under your control and you can make sure that both are build with the exact same compiler and compiler settings, it is possible to allocate something in the DLL and deallocate it in the executable.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    Re: allocation/deallocation of memory in exe and library

    The understand I have is a static library is a collection of compiled object files. Does not that mean that a static library could use a different CRT version than the exe?

    How can I check that within the application the exe and library use the same CRT? Using Windbg or process explorer I can obviously view which modules are loaded. This however does not tell me the relationship between the specific exe\dll and CRT. For instance, looking at the Outlook.exe it has loaded both msvcr80.dll and msvcr90.dll. I canot see which module uses which CRT.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  4. #4
    Join Date
    Apr 1999
    Posts
    27,429

    Re: allocation/deallocation of memory in exe and library

    Quote Originally Posted by laasunde View Post
    The understand I have is a static library is a collection of compiled object files. Does not that mean that a static library could use a different CRT version than the exe?
    Do you have a DLL or static library?

    If it is truly a static library, then you're reading too much into what a static library is. It is just a bunch of *.obj files in a single file. Nothing more, nothing less. Whatever options you used to compile each module that makes up the static library, that's what the library will contain. No different than if you didn't create a static library, and just built the files separately and linked them into your app.

    All aspects of that static library are gone when you run the executable, so there is no Windbg or other program that can help you. A static library's only context is when building the application, so you need to go to Visual C++, and check the properties of the files that make up the static library and see if they are all using the same CRT.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: allocation/deallocation of memory in exe and library

    Quote Originally Posted by laasunde View Post
    How can I check that within the application the exe and library use the same CRT? Using Windbg or process explorer I can obviously view which modules are loaded. This however does not tell me the relationship between the specific exe\dll and CRT. For instance, looking at the Outlook.exe it has loaded both msvcr80.dll and msvcr90.dll. I canot see which module uses which CRT.
    You can use a tool called XN resource editor. Open a .dll or .exe file and look under 'XP Theme Manifest' to view the exact version of the CRT that's used.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center