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. Sharing memory between dll and application with HeapAlloc ?

Sharing memory between dll and application with HeapAlloc ?

Scheduled Pinned Locked Moved C / C++ / MFC
comperformancequestion
3 Posts 2 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.
  • Y Offline
    Y Offline
    yarp
    wrote on last edited by
    #1

    Hi, I would like to do the following: Allocate datas in a dll and then free them in the main application. I have read there are potentials problems doing this because dll and process don't share the same memory, it depends on the compiler,... don't do this - ok. So I was looking for a solution and I went accross the Heap functions. It seems that when I allocate memory like this: HeapAlloc(GetProcessHeap(),... the default Heap is used to store the memory. Then the dll and my application will share the same Heap for that data. So they share the same Heap, but I still wonder if it safe is to call HeapAlloc from the dll and HeapFree from the application for the same data ? Any idea ?

    Yarp http://www.senosoft.com/

    L 1 Reply Last reply
    0
    • Y yarp

      Hi, I would like to do the following: Allocate datas in a dll and then free them in the main application. I have read there are potentials problems doing this because dll and process don't share the same memory, it depends on the compiler,... don't do this - ok. So I was looking for a solution and I went accross the Heap functions. It seems that when I allocate memory like this: HeapAlloc(GetProcessHeap(),... the default Heap is used to store the memory. Then the dll and my application will share the same Heap for that data. So they share the same Heap, but I still wonder if it safe is to call HeapAlloc from the dll and HeapFree from the application for the same data ? Any idea ?

      Yarp http://www.senosoft.com/

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      why can't you provide your own FreeMyDllObject() funtion inside the DLL, matching the way your DLL code allocates its stuff. Then the caller doesn't have to know the messy details, and there is less dependency between EXE and DLL. Later on, you could even change your DLL's memory management without changing the EXE at all. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Y 1 Reply Last reply
      0
      • L Luc Pattyn

        why can't you provide your own FreeMyDllObject() funtion inside the DLL, matching the way your DLL code allocates its stuff. Then the caller doesn't have to know the messy details, and there is less dependency between EXE and DLL. Later on, you could even change your DLL's memory management without changing the EXE at all. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Y Offline
        Y Offline
        yarp
        wrote on last edited by
        #3

        Hi Luc, There are various situations and memory is not always allocated in the same place. I have got 3 different case: - a DIB allocated in main thread and used in the dll - a DIB loaded in a thread used in the dll - a DIB loaded in a dll used in main thread I would need 3 different FreeMyObject for a unique DIB class while this HeapAlloc / HeapFree possible solution is common, and as simple as new / delete. I see it as a great simplification. Provided it is reliable on all systems. Thanks for your answer.

        Yarp http://www.senosoft.com/

        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