Search

programs using a static library and calling strdup() crash by attardi

Closed
as By Design Help for as By Design

0
0
Sign in
to vote
Type: Bug
ID: 333868
Opened: 3/21/2008 4:40:00 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
Any simple program that calls strdup() and links to a static library crashes with corrupted heap.

Notice that by replacing strdup() with _strdup() the program works, despite the fact that strdup() and _strdup() should be the same function: the documentation says that it is just a name change.
Details (expand)
Product Language
English

Version

Visual Studio 2008 (All Products and Editions)
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
Compile the following program:

#include <cstdlib>
#include <cstring>
#include "zlib/zlib.h"

void bar() { inflateInit2_(); }

int main(int argc, char* argv[])
{
char const* dup = strdup(argv[0]);
free((void*)dup);
}

Where the static library contains just one file, inflate.c:


#include <stddef.h>

int inflateInit2_()
{
return 0;
}

with the corresponding header zlib.h

#ifndef ZLIB_H
#define ZLIB_H

#ifdef __cplusplus
extern "C" {
#endif

extern int inflateInit2_();

#ifdef __cplusplus
}
#endif

#endif /* ZLIB_H */

Actual Results
The thread 'Win32 Thread' (0x7dc) has exited with code 8388608 (0x800000).
HEAP[VS08bug.exe]: Invalid Address specified to RtlValidateHeap( 003F0000, 00483178 )
Windows has triggered a breakpoint in VS08bug.exe.

This may be due to a corruption of the heap, which indicates a bug in VS08bug.exe or any of the DLLs it has loaded.

Moreover, if the #include <stddef.h> is removed, the application fails to start with the message:

This application has failed to start because MSVCR90.dll was not found.
Expected Results
Termination with no errors.
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
File Name Submitted By Submitted On File Size  
VS08bug.zip (restricted) 3/21/2008 -
Sign in to post a comment.
Posted by attardi on 4/3/2008 at 2:13 AM
Thank you for the reply.

Indeed compiling the program with /MD solves the problem.
But then this means I can never debug an application that uses a static library?

This used to be possible in VS 2005 (before SP1), despite the linker warning.

Moreover, why does _strdup() work and strdup() not?
This is at least very confusing, they should be the same function.
Posted by Microsoft on 4/2/2008 at 11:38 AM
This is by design. Beppe, you're compiling the static library with /MD and the program with /MDd. So, when you run the application, both the Retail CRT and the Debug CRT (msvcr90.dll and msvcr90d.dll) are loaded at the same time.

The error you're seeing:

indicated there are some problem in the heap.

If you put a breakpoint in the call to strdup in main() you will see this callstack (we're calling the Retail CRT msvcr90.dll)

>    msvcr90.dll!_strdup(const char * string=0x009c1b90) Line 66    C
    VS08bug.exe!main(int argc=1, char * * argv=0x009c1b88) Line 17 + 0xe bytes    C++
    VS08bug.exe!__tmainCRTStartup() Line 582 + 0x19 bytes    C
    VS08bug.exe!mainCRTStartup() Line 399    C
    kernel32.dll!BaseThreadInitThunk(unsigned long RunProcessInit=0, long (void *)* StartAddress=0x00000000, void * Argument=0x7efde000) Line 817 + 0x5 bytes    C
    ntdll.dll!_RtlUserThreadStart(long (void *)* StartAddress=0x0028111d, void * Argument=0x7efde000) Line 2695    C

but the free() goes into the Debug CRT (msvcr90d.dll):

>    msvcr90d.dll!free(void * pUserData=0x00031220) Line 48    C++
    VS08bug.exe!main(int argc=1, char * * argv=0x009c1b88) Line 18 + 0xc bytes    C++
    VS08bug.exe!__tmainCRTStartup() Line 582 + 0x19 bytes    C
    VS08bug.exe!mainCRTStartup() Line 399    C
    kernel32.dll!BaseThreadInitThunk(unsigned long RunProcessInit=0, long (void *)* StartAddress=0x00000000, void * Argument=0x7efde000) Line 817 + 0x5 bytes    C
    ntdll.dll!_RtlUserThreadStart(long (void *)* StartAddress=0x0028111d, void * Argument=0x7efde000) Line 2695    C

The two CRTs have 2 different heaps. msvcr90d.dll!free will try to verify if the memory was allocated in the msvcr90d.dll heap, but that check will fail:

>    msvcr90d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x00031220) Line 2103    C++
    msvcr90d.dll!_free_dbg_nolock(void * pUserData=0x00031220, int nBlockUse=1) Line 1317 + 0x9 bytes    C++
    msvcr90d.dll!_free_dbg(void * pUserData=0x00031220, int nBlockUse=1) Line 1258 + 0xd bytes    C++
    msvcr90d.dll!free(void * pUserData=0x00031220) Line 49 + 0xb bytes    C++
    VS08bug.exe!main(int argc=1, char * * argv=0x009c1b88) Line 18 + 0xc bytes    C++
    VS08bug.exe!__tmainCRTStartup() Line 582 + 0x19 bytes    C
    VS08bug.exe!mainCRTStartup() Line 399    C
    kernel32.dll!BaseThreadInitThunk(unsigned long RunProcessInit=0, long (void *)* StartAddress=0x00000000, void * Argument=0x7efde000) Line 817 + 0x5 bytes    C
    ntdll.dll!_RtlUserThreadStart(long (void *)* StartAddress=0x0028111d, void * Argument=0x7efde000) Line 2695    C

If you notice, the linker complained during compilation:

2>Linking...
2>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

If you compile the static library and the program with the same settings (both /MDd in Debug) then everything works ok.

HTH,
Ale Contenti
VC++ Libraries Dev Lead
Posted by Microsoft on 3/23/2008 at 8:20 PM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.