473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7964
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: TT (Tom Tempelaere) | last post by:
Hi people I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C int dma_start( const UCHAR* data, UINT data_length ) The function should start with a DMA...
1
by: Reynardine | last post by:
I am calling a C/C++ DLL from C# and I am marshalling the parameters to the API call by doing a type conversion for each parameter. For example, here is my C++ API method : short int XENO_API...
4
by: | last post by:
Hi, How do we marshall a type like this from a C++/CLI class wrapper to an unmanaged method? typedef struct { UINT32 blah : 1; UINT32 blah2 : 1; UINT32 blah3: 1;
2
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
3
by: awk | last post by:
Hi All I have a com dll written in VB6 (it's a User Function Library for my crystal reports - this allows me to write custom functions for Crystal which can be applied in Crystal formulas - none...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.