If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.

Re: allocation/deallocation of memory in exe and library

Originally Posted by laasunde

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

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.

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?

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.

Re: allocation/deallocation of memory in exe and library

Originally Posted by laasunde

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