all groups > dotnet interop > march 2005 >
You're in the

dotnet interop

group:

Memory gets freed intermittently



Memory gets freed intermittently TIL MSCOE
3/31/2005 10:41:01 PM
dotnet interop: I have a function which is something like this –
public unsafe int GenerateKey(out byte[] btKey)
{
//Method Variables
int iSuccess = -1;
btKey = null;
int iSymKeyLen = 0;

string strFileName = @"c:\DebugLog.txt";
FileStream fsLogFile = new
FileStream(strFileName,FileMode.Append,FileAccess.Write,FileShare.None;
StreamWriter swLogFile = new StreamWriter(fsLogFile);

try
{
//Declare an unmanaged pointer
sbyte* btOutKey = (sbyte*)null ;

//Calling the Managed Crypto function
ManagedEncryption objCrypto = new ManagedEncryption();

iSuccess = objCrypto.generateKey(&btOutKey,&iSymKeyLen);
btSymKey = new byte[iSymKeyLen];
if (iSuccess == 0)
{
//Copying the pointer value to out buffer System.IntPtr IRPtr =
(System.IntPtr)btOutKey;

// Get the contents of the btSymKey Byte Array before Marshal.Copy
for(int i=0;i<=btSymKey.Length-1;i++)
{
swLogFile.WriteLine("Before Marshal btSymKey:"+
btSymKey.GetValue(i).ToString() + " iSymKeyLen:" + iSymKeyLen.ToString());
}

if (btSymKey.Length > 0)
{
Marshal.Copy(IRPtr,btSymKey,0,iSymKeyLen);
}
else
{
swLogFile.Close();
fsLogFile.Close();
return -1;
}

//Free Memory
objCrypto.freeMemory(&btOutKey);
objCrypto = null;
}
return iSuccess;
}
catch (Exception ex)
{
swLogFile.WriteLine("An exception has occured:"+ ex.Message + ex.StackTrace);
throw;
}
finally
{
swLogFile.Close();
fsLogFile.Close();
}
}//end of GenerateSymKey function

Here in an unsafe method we are allocating a byte array using new. After the
allocation when we print the length we get it as 16. But in the for loop
where we try to print to contents of the array before copying any data to it
the loop executes for a few number of times only (less than 16) and after
that if when we try to print the length we get it as 0. It appears as if the
allocated memory is being garbage collected and freed…but how can this be
possible because it is still being referenced.

The calling function just declares a byte array and sets it to null and
calls the GenerateKey function.

byte[] btKey = null;
....
....
obj2.GenerateKey(out btKey);

This problem occurs sporadically on certain Win-98 machines and Windows XP
machines.
Re: Memory gets freed intermittently Mattias Sjögren
4/1/2005 11:02:58 PM
[quoted text, click to view]

Why are you doing that? A newly allocated array will have all its
elements initialized to the default value (zero in this case), so I
don't see why that's interesting.


[quoted text, click to view]

If that's true it's certainly a big problem. Can you come up with a
small complete app that reproduces this behavior?

Which framework version are you running? Do you have the latest
service pack installed? There have been a few JIT bugs that have
caused weird behavior but have been fixed.


[quoted text, click to view]

If you've gone through the trouble of writing a MC++ wrapper, why not
make it more managed code friendly by directly returning a byte[]
instead of requiring the client to use pointers in unsafe code?



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button