Im learning how to use C++ DLLs in C# and made a a c++ function that multiplies two allocated (Marshalled) set of variables. Everything works well in both C# and C++ until I increase the combined size of allocations to 1024 from 512MB. Then visual C# gives error of "protected memory access violation".The source of this is the dll function which fills the buffer with floats. The limit must be something between 512MB and 1024MB. Marshal.alloc accepts only int-sized buffer length so there is actually 2GB limit per allocation but when I try smaller chunks to get past the limit, gives same error.
Question: Is there any directbytebuffer equivalent that is not capped/limited in C# ? Or am I doing some simple pointer error?
Both dll and main projects are 64 bit targeted and can use more than 5-6 GB memory with ordinary arrays.
Here is the c++ function that writes to the buffer:
__declspec(dllexport) void floatOne(long av, int n)
{
float * vektor1=(float *)av;
_mm256_zeroall();
__m256 r0=_mm256_setr_ps(1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f,1.0f);
for(int i=0;i<n;i+=8)
{
_mm256_store_ps(vektor1+i, r0);
}
_mm256_zeroall();
return;
}
Here is how its used in C#:
public void one()
{
floatOne(bufferAdr.ToInt64() + offset, N);
// offset here is the properly aligned address to start usage
// N is private variable of vektor class (vector length)
}
Here is how allocation is:
public vektor(int n /* number of elements*/, int a /* alignmentı*/)
{
N = n;
bufferAdr = Marshal.AllocHGlobal(4*n + 4*a);
//a-1 was enough but I put a*4 to be sure it doesnt overflow.
offset = (a - bufferAdr.ToInt64() % a);
}
Here is the DLL importing:
[DllImport("cpuKullanim.dll", EntryPoint = "floatOne")]
public static extern void floatOne(long adres1, int n);
Tested RAM of any hardware errors but passed mem tests so there must be a software issue.
Thanks.
windows7-64 bit, cpu 64 bit, target machine 64bit for both projects.