Click to See Complete Forum and Search --> : Is this correct?
JamesSchumacher
February 15th, 2008, 04:28 PM
template <typename Type> inline void SerializeHelper(System::IO::BinaryWriter ^ bWriter,Type ^ obj)
{
cli::pin_ptr<Type> ptr = &(*obj);
unsigned char * pData = reinterpret_cast<unsigned char *>(ptr);
cli::array<unsigned char,1> ^ arData = gcnew cli::array<unsigned char,1>(sizeof(Type));
for (unsigned long i = 0; i < sizeof(Type); ++i)
{
arData[i] = pData[i];
}
bWriter->Write(arData,0,sizeof(Type));
}
Alex F
February 16th, 2008, 03:44 AM
I don't think that this is correct, because type may contain references to another types, which are copied to serialization stream and then restored as junk values.
There is standard documented way to serialize objects. Every type must control its serialization itself. The simplest way is to mark type with Serializable attribute. Do you have some specific reasons not to use standard serialization solutions?
JamesSchumacher
February 20th, 2008, 01:10 PM
I don't think that this is correct, because type may contain references to another types, which are copied to serialization stream and then restored as junk values.
There is standard documented way to serialize objects. Every type must control its serialization itself. The simplest way is to mark type with Serializable attribute. Do you have some specific reasons not to use standard serialization solutions?
I am aware of the object being able to hold references to other objects. I was wondering if this was the correct way to do a memory dump. That is what I meant. I would not use this in types that have references. I was merely wondering if this was correct for a managed type that stores nothing except POD values.
darwen
February 21st, 2008, 08:50 AM
For a memory dump you can do this... but DON'T USE THIS DATA TO DESERIALIZE THE OBJECT. I'm not sure exactly what is stored by .NET in a class instance block of memory - but this is internal to the .NET runtime and it shouldn't be assumed this won't change per version. Also there's bound to be more information stored in this block than in native C++ (metadata etc).
If you're only interested in looking at the memory then of course you can dump any block memory to a file - so long as it's pinned which is what you're doing.
Darwen.
P.S. Just for your information instead of doing
for (unsigned long i = 0; i < sizeof(Type); ++i)
{
arData[i] = pData[i];
}
you can use the methods in the Buffer class e.g. Buffer::BlockCopy. They're much more efficient for block memory copies (they don't do bounds checking of the index etc).
Alex F
February 22nd, 2008, 12:36 PM
Since you are using pin pointer, this looks correct.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.