Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memory allocation in dll

memory allocation in dll

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformancehelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NMiceli
    wrote on last edited by
    #1

    Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; }; and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure // CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; } loading the dll tagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll); but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in the new char[size], so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary?

    C C 2 Replies Last reply
    0
    • N NMiceli

      Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; }; and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure // CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; } loading the dll tagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll); but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in the new char[size], so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      you could do the allocation in the calling module, not in the DLL. or, just use a fixed-size buffer for the text in the structure. or, use global memory (ie. GlobalAlloc, etc) to allocate the strings. Image Toolkits | Image Processing | Cleek

      N 1 Reply Last reply
      0
      • C Chris Losinger

        you could do the allocation in the calling module, not in the DLL. or, just use a fixed-size buffer for the text in the structure. or, use global memory (ie. GlobalAlloc, etc) to allocate the strings. Image Toolkits | Image Processing | Cleek

        N Offline
        N Offline
        NMiceli
        wrote on last edited by
        #3

        Hi Chris, no it doesn't, i was trying to with HeapAlloc, but both after i call FreeLibrary(dll) i get a value. By the way, why it works in debug mode? anybody can give me links of dll tutorials/guides please. Best Regards

        1 Reply Last reply
        0
        • N NMiceli

          Hello. my problem is that i'm loading a dll to extract some data but it gets lost when i unload the library, i have the following struct struct tagPLUGIN{ PCHAR Name; PCHAR FileName; UINT Category; UINT Vital; ULONG Id; UINT isUI; }; and from my main exe i just load the library, get the proccess address and call a dll function to fill the structure // CODE IN DLL extern "C" __declspec(dllexport) void GetPlugInfo (tagPLUGIN *plugin) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); //plugin.FileName="" plugin->Category=TOOL; plugin->Id=0; plugin->isUI=TRUE; plugin->Name=new char[25]; strcpy(plugin->Name,"Configuración de Sistema\0"); plugin->Vital=FALSE; } loading the dll tagPLUGIN plugin; typedef void (*GetPlugInfo)(tagPLUGIN *info); HMODULE dll=LoadLibrary(tmp_string); GetPlugInfo getPlugInfo = (GetPlugInfo)GetProcAddress(dll,"GetPlugInfo"); (getPlugInfo)(&plugin); plugin.Id=ID++; plugin.FileName=new char[tmp_string.GetLength()+1]; strcpy(plugin.FileName,tmp_string.GetBuffer()); FreeLibrary(dll); but right after FreeLibrary(dll) my two PCHAR values are corrupt, i know the problem is in the new char[size], so how can i pass a PCHAR to a dll function and allocate and assign a string without loosing it after freelibrary?

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          Your dll, and or exe, is likely being staticly linked to the CRT. That is, the dll new, malloc, alloc, calloc functions are using a heap local to the dll. When the dll is unloaded the heap is destroyed. The debug version is likely set to dynamicly link to the CRT so the exe and dll use the same heap for memory allocations. ...cmk Save the whales - collect the whole set

          A 1 Reply Last reply
          0
          • C cmk

            Your dll, and or exe, is likely being staticly linked to the CRT. That is, the dll new, malloc, alloc, calloc functions are using a heap local to the dll. When the dll is unloaded the heap is destroyed. The debug version is likely set to dynamicly link to the CRT so the exe and dll use the same heap for memory allocations. ...cmk Save the whales - collect the whole set

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Tthanks for your help. No, i'm using both debug and release in shared dll, the dll and exe both debug uses debug and release uses release, i've tried, new,malloc,GlobalAlloc and Heap Alloc, same result. i'll do some more reading about dlls and retry this, but for now i have an idea, can you tell me if this could be the good way: near that code i have some code to read from the registry and i've noted that when in RegQueryValueEx you pass a buffer too small for the required data (REG_SZ) it returns the size of the buffer needed so you can reallocate the memory for the buffer with that size, so i'll code my GetPlugInfo in that way and i'll pass the PCHAR vars with the memory preallocated. Is that a good solution? Thanks in advance.

            C 1 Reply Last reply
            0
            • A Anonymous

              Tthanks for your help. No, i'm using both debug and release in shared dll, the dll and exe both debug uses debug and release uses release, i've tried, new,malloc,GlobalAlloc and Heap Alloc, same result. i'll do some more reading about dlls and retry this, but for now i have an idea, can you tell me if this could be the good way: near that code i have some code to read from the registry and i've noted that when in RegQueryValueEx you pass a buffer too small for the required data (REG_SZ) it returns the size of the buffer needed so you can reallocate the memory for the buffer with that size, so i'll code my GetPlugInfo in that way and i'll pass the PCHAR vars with the memory preallocated. Is that a good solution? Thanks in advance.

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              I'm not sure that you understood what i was talking about. In VC++ Goto Projects - Settings. Goto C/C++ - Code Generation. For both your exe and dll the 'Use run-time library' should be: 'Debug Multithreaded DLL' for your debug builds. and 'Multithreaded DLL' for your release builds. There is no reason you can't do what you originally specified. ...cmk Save the whales - collect the whole set

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups