I have simple shared memory DLL for interprocess data exchange from unmanaged to managed app.
And I noticed that my managed app memory is growing in size steadily.
Can someone advise what can be the cause, how to find it and how to fix it?
Relevant parts of codes below.
cpp SharedMem.DLL:
#pragma data_seg( ".IPC" )
....
double darr[MAXITEMS] = { 0.0 } ;
....
#pragma data_seg()
....
double __stdcall MGetDouble (int idx)
{
if ( idx>= 0 && idx < MAXITEMS)
{
return darr[idx];
}
else
{
return -1.0 ;
}
}
int __stdcall MSetDouble (int idx, double dvalue)
{
if ( idx>= 0 && idx< MAXITEMS)
{
darr[idx] = dvalue;
return idx;
}
else
{
return -1;
}
}
and c# app:
[DllImport("SharedMem.DLL", CallingConvention = CallingConvention.StdCall)]
public static extern double MGetDouble(int index);
....
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(dosmth);
}
public object lockobj = new object();
public void dosmth(object o)
{
if (Monitor.TryEnter(lockobj, 50))
{
....
double[,] matrix = new double[size, TSIZE];
....
double gvd;
int k;
for (int i = 0; i < lines; i++)
for (j = 0; j < TSIZE; j++)
{
k++; //k can be up to 2k-4k typically
gvd = MGetDouble(k);
matrix[i, j] = gvd;
}
//... do the stuff
Monitor.Exit(lockobj);
}
}
Try this aproach:
and alter your “outer” do smth: