It is possible to delete SharedMemory from another Application??
-
Freitag, 20. April 2012 02:47
I have created DLL to open and manage shared memory.The shared memory is created using CreateFileMapping(), MapViewOfFile(). Then I loaded the DLL using MFC application and Read/Write performed to Shared memory using shared pointer returned by DLL( via MapViewOfFile()). The read/write operations performed with the MFC app is working fine. But I got an exception when tried to delete the shared pointer directly from the MFC app. Could you please help me to find the reason why exception thrown for shared pointer deletion from the application?It is supposed to release using UnMapViewOfFile() only?I can delete if the DLL returns a normal pointer.
Thanks in advance
Alle Antworten
-
Freitag, 20. April 2012 04:22
Jaydhar wrote:
I have created DLL to open and manage shared memory.The shared memory is created using CreateFileMapping(), MapViewOfFile(). Then
I loaded the DLL using MFC application and Read/Write performed to Shared memory using shared pointer returned by DLL( via
MapViewOfFile()). The read/write operations performed with the MFC app is working fine. But I got an exception when tried to
delete the shared pointer directly from the MFC app.What do you mean, delete? Do you mean this literally, as in "delete myPointer;" ? Of course that won't work - you can only deallocate with delete what you allocated with new (and deallocate with free() what you allocated with malloc(); and deallocate with HeapFree what you allocated with HeapAlloc; and so on and so forth). Allocation and deallocation functions come in pairs, you can't mix and match them.
It is supposed to release using UnMapViewOfFile() only?
Yes.
I can delete if the DLL returns a normal pointer.
What's a "normal pointer"? I'm not familiar with the term. Are there abnormal ones?
Igor Tandetnik
- Als Antwort vorgeschlagen Helen ZhaoModerator Montag, 23. April 2012 05:57
- Als Antwort markiert Helen ZhaoModerator Freitag, 27. April 2012 05:46
-
Freitag, 20. April 2012 06:52
Hi Igor,
Thank you for the reply.
What I meant by delete pointer is to free the memory which is allocated either by malloc or heapalloc. Previously I gt confused with the pointer to Shared memory and now I understood the difference.
The normal pointer which points somewhere in memory.It is simple and nothing abnormal:). Hopefully it'll cleared by owner at some point.Other kinds are smart pointer and shared pointer.
Jay
-
Freitag, 20. April 2012 12:27
-
Samstag, 21. April 2012 13:56
check this link this may help you out
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/b3cf8d9b-187d-4895-b831-89dbb1897b0a
- Als Antwort vorgeschlagen Helen ZhaoModerator Montag, 23. April 2012 05:58
- Als Antwort markiert Helen ZhaoModerator Freitag, 27. April 2012 05:46
-
Montag, 23. April 2012 01:32
If the pointer returned by MapViewOfFile can be called as Normal then why should the de-allocation failed using delete operator?.So it behaves like special and difficult to manage as normal. As discussed earlier de-allocation works fine for those which is allocated using Malloc and HeapAlloc. But we used MapViewOfFile to reserve the pages in virtual address range (using VirtualAlloc )and is unavailable to other allocations. The reserved pages can only be freed using the VirtualFree API. Reserved are released when the view is unmapped and the file mapping object is closed. Actually this is the only point I want to know. Thanks for your help.
-
Montag, 23. April 2012 01:34
Hi Kevin,
Thanks for your help.
The link you put seems my question asked in the same thread:).
Jay
-
Montag, 23. April 2012 02:04
Jaydhar wrote:
If the pointer returned by MapViewOfFile can be called as Normal then why should the de-allocation failed using delete
operator?Because the memory wasn't allocated with new operator.
So it behaves like special and difficult to manage as normal.
This is begging the question. You seem to have changed your definition of the term "normal pointer" (to mean "a pointer to memory allocated with new", as far as I can tell), introduced a new term "special pointer" (to mean "a pointer to memory allocated by any menas other than new", apparently), and then ask why special pointers don't behave like normal pointers.
As discussed earlier de-allocation works fine for those
which is allocated using Malloc and HeapAlloc.It is possible that, in a particular implemenation, free() simply delegates to HeapFree, and operator delete simply delegates to free() (and so ultimately calls HeapFree). This is an internal undocumented implementation detail subject to change without notice (and in fact, this has not always been the case, and is not the case now under certain configuration settings). It is a bad idea to rely on being able to mix and match allocation and deallocation functions this way.
But we used MapViewOfFile to reserve the pages in virtual address range (using
VirtualAlloc )I don't understand this statement. What exactly is the relationship between MapViewOfFile and VirtualAlloc in your program? How are the two combined?
The reserved pages can only be freed using the VirtualFree API
As usual, allocation and deallocation functions come in pairs. Memory allocated with VirtualAlloc should be deallcoated with VirtualFree. This should come as no surprise by now.
Igor Tandetnik
- Als Antwort vorgeschlagen Helen ZhaoModerator Montag, 23. April 2012 05:58
- Als Antwort markiert Helen ZhaoModerator Freitag, 27. April 2012 05:46

