在WIN32中用API GlobalAlloc函数分配的内存可以由多个进程进行共享,但如果分配内存的进程退出了并没有释放这块内存,系统是否会自动释放这块内存?
如果会自动释放,那如果分配内存的进程异常中止的,岂不给所有共享这块内存的进程带来了极大的安全隐患?
但如果不会自动释放,那到最后由谁释放呢?

解决方案 »

  1.   

    GlobalAlloc是从全局堆中分配内存空间,在GlobalFree时才会释放,而如果不释放,则会造成全局堆内存泄露。
      

  2.   

    GlobalAlloc:函数分配一块内存,该函数会返回分配的内存句柄。
    GlobalFree:函数来释放内存块。您必须传给该函数一个内存句柄。
      

  3.   

    LZ说的问题,不知道通过GlobalHandle是否可以跨进程得到堆内存块的句柄,再由外进程GlobalFree
      

  4.   

    在Win32中,每个进程都只拥有一个省缺的私有堆,它只能被当前进程访问。应用程序也不可能直接访问系统内存。所以在Win32中全局堆和局部堆都指向进程的省缺堆。用LocalAlloc/GlobalAlloc分配内存没有任何区别。甚至LocalAlloc分配的内存可以被GlobalFree释放掉。
      

  5.   


    我刚才专门做了个试验,当分配内存的进程退出了,其它进程仍然可以使用共享的内存.说明内存并没有释放.
    那是不是如果一直不调用GlobalFree,是不是此处内存就一直得不到释放吗?
      

  6.   


    不会的,WINDOWS的内存管理只会负责合理分配、配置内存空间、响应应用请求,如果不释放,肯定要造成全局堆上的死区。
      

  7.   


    是的,但是注销是否有效果我不知道,我只知道从原理上说会泄露,但通过系统自己,能够在什么节点上,或通过什么手段触发,能够释放从而恢复已申请全局堆内存块,我不知道。如果GlobalHandle的方式不可行,你可以问问主席有什么手段。
      

  8.   

    先引用几段MSDN:The global and local functions are supported for porting from 16-bit code, or for maintaining source code compatibility with 16-bit Windows. The global and local functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions. For a complete list of functions, see the table in Memory Management Functions.Windows memory management does not provide a separate local heap and global heap, as 16-bit Windows does. As a result, the global and local families of functions are equivalent and chosing between them is a matter of personal preference. Note that the change from a 16-bit segmented memory model to a 32-bit virtual memory model has made some of the related global and local functions and their options unnecessary or meaningless. For example, there are no longer near and far pointers, because both local and global allocations return 32-bit virtual addresses.Memory objects allocated by GlobalAlloc and LocalAlloc are in private, committed pages with read/write access that cannot be accessed by other processes. Memory allocated by using GlobalAlloc with GMEM_DDESHARE is not actually shared globally as it is in 16-bit Windows. This value has no effect and is available only for compatibility. Applications requiring shared memory for other purposes must use file-mapping objects. Multiple processes can map a view of the same file-mapping object to provide named shared memory. For more information, see File Mapping.Memory allocations are limited only by the available physical memory, including storage in the paging file on disk. When you allocate fixed memory, GlobalAlloc and LocalAlloc return a pointer that the calling process can immediately use to access the memory. When you allocate moveable memory, the return value is a handle. To get a pointer to a movable memory object, use the GlobalLock and LocalLock functions.
      

  9.   

    GlobalAlloc等函数主要是用来兼容16位程序的。Win32程序并没有全局堆这个概念,申请到的地址空间全部都是虚拟地址空间。外部程序不能直接访问。
    至于为什么可以共享,我认为是系统为另外的进程同样创建了一份内存映射,指向相同的物理地址。
    当程序关闭时,进程的资源被释放。但是内存空间还被其他程序使用,因此,只是将对应的引用计数-1。在没有进程使用这块内存后,内存被系统释放。
      

  10.   

    也就是说WIN32上不存在真正的共享内存?
      

  11.   

    没有真正的共享内存。地址空间进程私有。Win32上使用共享内存,可以使用内存映射来实现。