In answer to the direct question, you don't need to clean up anything
yourself. The string is an object that the GC will clean up. The struct
is
a value type that will be on the stack and doesn't need you to do any
cleanup either.
That said, your code is a recipe for disaster. You have a pointer that
is
fixed only in the context of your MarshalStrData function. You return
and
it's no longer fixed, so a compaction could easily move the string that
it
"points" to and the pointer is then invalid and you get an access
violation
if you're lucky or random behavior if you're not.
Also a BSTR in native code is most certainly *not* the same as a string
or a
char * in managed code.
This is bad code. You need to learn about and use a GCHandle. Looking
at
how data types are marshaled would also be a good idea.
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
MarshalStrData() is the custom method which marshals the string data
into
char*
like
unsafe private char* MarshalStrData(String str)
{
fixed(char* ch = str.ToCharArray())
{
return ch;
}
}
Now I want to know that who is responsible for destroying the Employee
struct and its member(like char* FirstName, etc) which i passes from c#
to
c++? it is automatically Garbage Collect or explicitly calls the
release/delete method on them
:
What is MarshalStrData?
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
Here is the sample code which i used
// .NET structure
unsafe public struct Employee
{
public char* FirstName;
public char* LastName;
public char* Address;
}
[DllImport("test.dll")]
private static extern int SetEmployeeData(ref Employee item);
static void Main(string[] args)
{
try
{
Employee emp = new Employee();
emp.FirstName = MarshalStrData("john");
emp.LastName = MarshalStrData("simon");
emp.Address = MarshalStrData("Ca. US");
SetEmployeeData(ref emp);
}catch(Exception)
{
}
}
// C++ Structure
struct Employee
{
BSTR FirstName;
BSTR LastName;
BSTR Address;
}
// C++ declaration
public int SetEmpoyeeData(*Employee item)
{
// do some Poom related code
// release poom
}
-sohail
:
First of all a BSTR and a char* are _NOT_ the same, so your code is
incorrect. Second, it's a value type, so inherently there's
nothing
to
collect, the 12 bytes you allocate will get freed when the struct
goes
out
of scope. However, since these are pointers to some memory, you
must
be
making an allocation somewhere and getting a pointer to that
allocated
area
to put into your struct. Those allocations need to be freed. I
can't
tell
you how that's done becasue you've now shown the allocation code.
If you have control over that target API and it's not already
locked
down
by
a spec of native consumer, I'd recomment not using BSTRs. A TCHAR
*
is
going to be easier to deal with.
--
Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com
Hi,
I'm developing the pocket pc application in which i pass the
unsafe
structure to c++. i see that if i pass 10 times data strucure it
consumes
more memory. i have the structure like
// .NET structure
unsafe public struct Employee
{
public char* FirstName;
public char* LastName;
public char* Address;
}
[DllImport("test.dll")]
private static extern int SetEmployeeData(ref Employee item);
// C++ Structure
struct Employee
{
BSTR FirstName;
BSTR LastName;
BSTR Address;
}
// C++ declaration
public int SetEmpoyeeData(*Employee item)
[
// do some code
}
Can anyone please let me know how to delete or garbage collect
this
data
structure in managed code like delete function in c++.?
Thanks