473,231 Members | 1,439 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,231 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 7943
"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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.