471,075 Members | 720 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Marshalling: How to delete dynamic array from managed code.

Hi,

My native C++ function creates a dynamic array. I'm marshalling it to
managed code and got to delete after. How to make it:

c++ code:

void CreateArrayInside( pTestStruct &TestStruct,
unsigned int &size)
{
size = 5000;

// test structure
TestStruct = new CTestStruct[size];
}

c# declaration:
[DllImport( "MathDll.dll", CharSet = CharSet.Auto, EntryPoint =
"CreateArrayInside" )]
public static extern void CreateArrayInside( ref IntPtr
TestStructArray, out uint size );

c# usage:
IntPtr pTestStructs = new IntPtr();
uint Count;

for ( uint i = 0; i < 20000; i++ )
{
MathDLLWrapper.Interface.CreateArrayInside( ref pTestStructs, out
Count );

// Marshal.FreeCoTaskMem( pTestStructs );

Console.WriteLine("Loop: " + i);
}

I tried to use Marshal.FreeCoTaskMem( pTestStructs ) but memory get
leaks. As it is said in the msdn the FreeCoTaskMem got to be used for
to free up memory allocated for COM tasks.

Is there any way to free up memory allocated by C++ part?

Please give an advise.

Thanks,
Eugene.

Marshal.FreeHGlobal( pTestStructs );

Jan 12 '07 #1
6 7665
"Ananas" <re****@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hi,

My native C++ function creates a dynamic array. I'm marshalling it to
managed code and got to delete after. How to make it:

c++ code:

void CreateArrayInside( pTestStruct &TestStruct,
unsigned int &size)
{
size = 5000;

// test structure
TestStruct = new CTestStruct[size];
}

c# declaration:
[DllImport( "MathDll.dll", CharSet = CharSet.Auto, EntryPoint =
"CreateArrayInside" )]
public static extern void CreateArrayInside( ref IntPtr
TestStructArray, out uint size );

c# usage:
IntPtr pTestStructs = new IntPtr();
uint Count;

for ( uint i = 0; i < 20000; i++ )
{
MathDLLWrapper.Interface.CreateArrayInside( ref pTestStructs, out
Count );

// Marshal.FreeCoTaskMem( pTestStructs );

Console.WriteLine("Loop: " + i);
}

I tried to use Marshal.FreeCoTaskMem( pTestStructs ) but memory get
leaks. As it is said in the msdn the FreeCoTaskMem got to be used for
to free up memory allocated for COM tasks.

Is there any way to free up memory allocated by C++ part?

Please give an advise.

Thanks,
Eugene.

Marshal.FreeHGlobal( pTestStructs );

You have to provide a C++ function that deletes the unmanaged memory allocated by the new
operator, the managed code has to call this function when done with it.

Willy.

Jan 12 '07 #2
I did provided this function, it looked like this:

void DeletePtr(void *Ptr)
{
delete[] Ptr;
}

but I get a memory error in managed part.

Could you say what is incorrect?

"""Willy Denoyette [MVP] ΠΙΣΑΜ(Α):
"""
"Ananas" <re****@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hi,

My native C++ function creates a dynamic array. I'm marshalling it to
managed code and got to delete after. How to make it:

c++ code:

void CreateArrayInside( pTestStruct &TestStruct,
unsigned int &size)
{
size = 5000;

// test structure
TestStruct = new CTestStruct[size];
}

c# declaration:
[DllImport( "MathDll.dll", CharSet = CharSet.Auto, EntryPoint =
"CreateArrayInside" )]
public static extern void CreateArrayInside( ref IntPtr
TestStructArray, out uint size );

c# usage:
IntPtr pTestStructs = new IntPtr();
uint Count;

for ( uint i = 0; i < 20000; i++ )
{
MathDLLWrapper.Interface.CreateArrayInside( ref pTestStructs, out
Count );

// Marshal.FreeCoTaskMem( pTestStructs );

Console.WriteLine("Loop: " + i);
}

I tried to use Marshal.FreeCoTaskMem( pTestStructs ) but memory get
leaks. As it is said in the msdn the FreeCoTaskMem got to be used for
to free up memory allocated for COM tasks.

Is there any way to free up memory allocated by C++ part?

Please give an advise.

Thanks,
Eugene.

Marshal.FreeHGlobal( pTestStructs );


You have to provide a C++ function that deletes the unmanaged memory allocated by the new
operator, the managed code has to call this function when done with it.

Willy.
Jan 13 '07 #3
"Ananas" <re****@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>I did provided this function, it looked like this:

void DeletePtr(void *Ptr)
{
delete[] Ptr;
}

but I get a memory error in managed part.

Could you say what is incorrect?
You new'd an object in C++, it's up to delete this object in C++, no need to pass the
pointer to and from managed code.

void DeletePtr()
{
if(TestStruct)
delete[] TestStruct;
}

Willy.

Jan 13 '07 #4
But if I want to pass a pointer from managed memory how should I go?
Actually it is not good to create for each pointer function deleting
the memory.
"""Willy Denoyette [MVP] ΠΙΣΑΜ(Α):
"""
"Ananas" <re****@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
I did provided this function, it looked like this:

void DeletePtr(void *Ptr)
{
delete[] Ptr;
}

but I get a memory error in managed part.

Could you say what is incorrect?

You new'd an object in C++, it's up to delete this object in C++, no need to pass the
pointer to and from managed code.

void DeletePtr()
{
if(TestStruct)
delete[] TestStruct;
}

Willy.
Jan 13 '07 #5
"Ananas" <re****@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
But if I want to pass a pointer from managed memory how should I go?
Actually it is not good to create for each pointer function deleting
the memory.
I really don't know what you are trying to achieve, but following should work....

// CS file

[DllImport("xxxxx.dll"), SuppressUnmanagedCodeSecurity]
static extern void CreateArrayInside(out IntPtr d, out int size);
[DllImport("xxxxx.dll"), SuppressUnmanagedCodeSecurity]
static extern void DeleteArray(IntPtr d);
static void Main()
{
IntPtr ptr = IntPtr.Zero;
int size;
GetArray(out ptr, out size);
// access data through unmanaged pointer here.... this will require Marshal.xxxxx calls
...
DeleteArray(ptr);
}
}

// CPP file
struct MyStruct {
long l;
unsigned char c;
};
extern "C" void __declspec(dllexport) __stdcall CreateArrayInside(MyStruct **ptr, unsigned
int &size)
{
size = 100;
*ptr = new MyStruct[size];
ptr[0]->l = 123456;
}

// delete memory pointed by ptr, oh what do I hate these void* ........
extern "C" void __declspec(dllexport) __stdcall DeleteArray(void *ptr)
{
if(ptr)
delete[] ptr;
}

Willy.

Jan 13 '07 #6
OK, thank you, I got it working.
"""Willy Denoyette [MVP] ΠΙΣΑΜ(Α):
"""
"Ananas" <re****@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
But if I want to pass a pointer from managed memory how should I go?
Actually it is not good to create for each pointer function deleting
the memory.

I really don't know what you are trying to achieve, but following should work....

// CS file

[DllImport("xxxxx.dll"), SuppressUnmanagedCodeSecurity]
static extern void CreateArrayInside(out IntPtr d, out int size);
[DllImport("xxxxx.dll"), SuppressUnmanagedCodeSecurity]
static extern void DeleteArray(IntPtr d);
static void Main()
{
IntPtr ptr = IntPtr.Zero;
int size;
GetArray(out ptr, out size);
// access data through unmanaged pointer here.... this will require Marshal.xxxxx calls
...
DeleteArray(ptr);
}
}

// CPP file
struct MyStruct {
long l;
unsigned char c;
};
extern "C" void __declspec(dllexport) __stdcall CreateArrayInside(MyStruct **ptr, unsigned
int &size)
{
size = 100;
*ptr = new MyStruct[size];
ptr[0]->l = 123456;
}

// delete memory pointed by ptr, oh what do I hate these void* ........
extern "C" void __declspec(dllexport) __stdcall DeleteArray(void *ptr)
{
if(ptr)
delete[] ptr;
}

Willy.
Jan 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by TT (Tom Tempelaere) | last post: by
2 posts views Thread by BartMan | last post: by
3 posts views Thread by NateDawg | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.