Click to See Complete Forum and Search --> : Delete object allocated in shared memory


Fandu_Nagesh
December 15th, 2004, 10:44 AM
Hi Guru,
I am using shared memory as IPC mechanism to share data between two process.
I am using following set of function in sequence.
m_dwSize = 256
m_hmap = CreateFileMapping(0, 0, PAGE_READWRITE, 0, m_dwSize, "nagesh");
void *vdPtr = (void*) MapViewOfFile(m_hmap,FILE_MAP_ALL_ACCESS, 0,0, m_dwSize);
CMyClass *myPtr = new(vdPtr) CMyClass; //Using Placement new
.....

...

UnmapViewOfFile(myPtr)
CloseHandle(m_hmap)


Above code shows that the Object is allocated on the specified shared memory address.
How shall I call the delete on myPtr(CMyClass object).

Regards
Nagesh

kirants
December 15th, 2004, 01:33 PM
CMyClass *myPtr = new(vdPtr) CMyClass; //Using Placement new


Well, this memory is on the heap, right ? It is not shared memory .. .

Paul McKenzie
December 15th, 2004, 01:53 PM
Hi Guru,
Above code shows that the Object is allocated on the specified shared memory address.I am assuming you're right.
How shall I call the delete on myPtr(CMyClass object).Just like any other object that has been created with placement-new. You explicitly call the destructor.

myPtr->~CMyClass();

Regards,

Paul McKenzie

Andreas Masur
December 16th, 2004, 04:12 AM
Well, this memory is on the heap, right ? It is not shared memory .. .
Nope....take a look at the comment...he is using 'placement new'... :cool:

Fandu_Nagesh
December 16th, 2004, 04:18 AM
Thanks Paul and Andreas,

Well, I have anather question in my mind.
As you all aware that UnmapViewOfFile deletes the shared page only when all the handles pointed to it are de-referenced. i.e at closure of LAST handle.
Hence, I need to call the destructor only when you are calling LAST UnmapViewOfFile.

How shall I find that the call to UnmapViewOfFile is going to delete the shared page and hence this is the right time to call the destructor.

Please help me in this regard.
Thanks in advance.

Regard
Nagesh :blush:

Andreas Masur
December 16th, 2004, 04:31 AM
Well...in your case you should be safe when you do not have any view mapped any longer...in other words...if everything is unmapped...delete your object as well...

Fandu_Nagesh
December 16th, 2004, 04:49 AM
Yes Andreas,
Just I want to know whether there is any way to find out that UnmapViewOfFile is going to delete the shared page. If I am able to find out this prior to UnmapViewOfFile then I could call destructor to cleanup the things.

Basically, I need to find out how many handles are still pointing to the shared page. We can infer destructor calling logic from this.

I have another idea like, I will maintain the REFERENCE COUNT which will increment on CreateFileMap/OpenFileMap and decrement on UnMapView File. This number will give me the number of open handles. What you think?

If Windows OS provides this information to me....It will be great....
Thanks
Nagesh

Andreas Masur
December 16th, 2004, 05:58 AM
I do not know of any functionality that gives you the number of remaining handles...and most-likely such a function would need another handle as well...so it probably would be the chicken and egg thingie...

At the end...the operating system will take care about it anyway...as long as you make sure that your application does not have any view mapped, you can safely delete the object....no mapped view...no object needed...

Fandu_Nagesh
December 17th, 2004, 08:01 AM
Thanks Andreas!!!!

I think I need to explicitly maintain the Referece Count for the Open and close handles.

I am just wondering why Winodws is not providing this information with UnMapView....

Thanks
Nagesh

Andreas Masur
December 17th, 2004, 10:15 AM
I think I need to explicitly maintain the Referece Count for the Open and close handles.

I am just wondering why Winodws is not providing this information with UnMapView....
Well...basically windows already does it with handles (as well as with other objects), however, that is only for the operating system. In your case you need to separate between windows handles and your data in the application. Windows will take about the handles (since they are part of the operating system), you need to take care about the data in your application (since Windows cannot know anything about them).

Thus....as being mentioned....don't care about the operating system in this case, make sure that you application data will be handled correctly...the operating system will take care about the rest... :cool:

phoebus21
December 18th, 2004, 08:26 AM
Ok,
just for safety's sake what is this 'placement new' ?
regards,
phoebus.

Andreas Masur
December 18th, 2004, 03:26 PM
just for safety's sake what is this 'placement new' ?

What is "placement new" and why would I use it? (http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10)...