c++ - Heap allocation failing in user DLL/EXE -




properly linked dlls , exes supposed have 1 freestore can allocate heap-based objects. here chis becke’s answer in who allocates heap dll?:

… c++ runtime responsible creating freestore , deciding how allocate it. specifically, if use dll runtime option, single dll - msvcrtxx.dll - manages single freestore shared between dll's, , exe, linked against dll

since true, should able new objects in dll/exes defined in other dll/exes. according chris, msvcrtxx.dll , compile-time/runtime linker take care of joint freestore dll/exes can obtained.

that not working me.

to test this, have generated 2 mfc dialog programs: newfailmfc1 , newfailmfc2. running newfailmfc2 accesses newfailmfc1’s www function fails when doing new.

// code in newfailmfc1. void www() {   char* ch { nullptr };   ch = new char[ 100 ]; // error: attempts allocate memory somewhere else in prescribed joint dll/exe freestore   ch[ 0 ] = '\0'; }  // calling code in newfailmfc2. www(); 

does better knowledge of how dll/exe freestore works me know problem is?

(i attempted ask question once before in "global function ::operator new fails when compiled in myapp1 , myapp2. during asking process, discovered problem occurring more in <random> std lib.)

edit1:

in msdn nice virtual agent found potential errors passing crt objects across dll boundaries me. unfortunately, solution recommends compiling programs /md compiler option, , not /mt uses multiple copies of crt automatically leads crossing boundaries , memory access violations.

this not news app developer me. need best practice can apply , meet delivery deadlines without having deal arcane low-level memory problems. how fx know there hidden call global ::operator new in std:random_device type? wouldn’t until access-violated. after research realize calling global new, crossing boundary gave dll/exe access violation. obscure.

edit2:

i have submitted bug report in visual studio regarding std::random_device implementation. see "std::random_device instantiation causes access-violation in cases".

it's possible cross boundaries whatever means :) first of all, need understand what's going on.

when allocate memory, crt can allocate little bit more asked. e.g. popular practice (at least in past) allocate 4 bytes more (substitute system bitness), write size of allocated memory @ beginning , return ptr + 4 you. when release memory system knows how should release.

it's bit simplified picture. different compilers, different versions of same compiler , different configs of same compiler same version can differently. e.g. debug config can use padding detect buffer overruns, , other tricks. when allocate memory in 1 binary , deallocate in another, if different compilers used can lead corrupted memory (immediate crash in best case).

this , many other reasons lead common suggestion: release memory in binary allocated it. achieved providing release member function of api class , making destructor private, or unique_ptr (or shared_ptr) custom deleter, or other techniques.

now /md suggestion. /md means dynamic crt (= in dll), , it's not possible load same dll twice in same process means same crt used allocation , deallocation. still not solution different versions or different compilers. e.g. many applications use plugins system, in case it's not idea demand plugins compiled specific compiler/version/config





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -

python - Read npy file directly from S3 StreamingBody -