Original Post
Hi, I'm in a bit over my head. I'd appreciate any sort of ideas concerning my problem. Why do I get memory alignment crashes if I load and use a MinGW DLL in an application compiled with VC++ (Express 2005), but not if I compile the same application (.cpp file) with MinGW? I'm developing a DLL that wraps a 3rd party library (FFMPEG, used for video encoding). I have a simple interface to my DLL, basically I just pass in a FFMPEG command line (char*) and all the heavy processing is done in the DLL function (which in turn uses functionality from FFMPEG DLLs). I've compiled my DLL and FFMPEG using MinGW (the only windows compiler supported by FFMPEG). I've created a simple test application which encodes a bunch of videos (30 of them) in a loop. I've compiled this test application in both MinGW and in VC++. The MinGW version runs fine, with or without SSE optimized routines. The VC++ version only works if I run-time disable FFMPEG's SSE/MMX optimized routines (via a command line parameter). If I don't do this, I get a SEGFAULT in a SSE2 (Discreet Cosine Transform - DCT) routine. This seems to be related to memory not being aligned properly (16 byte alignment is required for SSE2 I believe). I don't pass any memory across the DLL boundary that's used directly by any of the FFMPEG routines. The DLL is pretty self contained. So, it's not my test application that supplies any unaligned memory, causing the crash. Any ideas what this problem might be? Why would the DLL loaded in a VC++ app mess up memory alignment? I can add that in the MinGW application the SSE2 code isn't much faster than the non-SEE-optimized one, the 30 videos are encoded in 40 vs 42 seconds. So, I could disable it if it wasn't for the fact that in the VC++ compiled application the non-SSE-optimized code suddenly takes 53 seconds, almost ~25% longer time. The application loop has no real processing overhead, so this is just the DLL function running 25% slower. My guess is that this could also be due to memory misalignments. It works, but at reduced performance. I'm not quite sure if it's misaligned memory on the heap or on the stack, debugging with GDB doesn't make me much wiser. Although I wonder why the block1* pointer, which is set to point to some (hopefully) aligned stack memory, is reported to be set to 0x3. SOME GDB INFO: -------------- Program received signal SIGSEGV, Segmentation fault. 0x6875b33e in ff_fdct_sse2 (block=0x3f1e0) at i386/fdct_mmx.c:369 (gdb) print block $4 = (int16_t *) 0x3f1e0 (gdb) print block1 $5 = (int16_t * const) 0x3 (gdb) print align_tmp $6 = {0 } SOURCE CODE: ------------
void ff_fdct_sse2(int16_t *block)
{
int64_t align_tmp[16] ATTR_ALIGN(16);
int16_t * const block1= (int16_t*)align_tmp;
fdct_col_sse2(block, block1, 0); // <---- SEGFAULT calling this
fdct_row_sse2(block1, block);
}