How do I delete or garbage collect the unmanaged structure in mana

G

Guest

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
 
P

Paul G. Tobey [eMVP]

Huh? If you're passing a C# structure to native code, it will be garbage
collected along with any other managed code data at a time chosen by the
garbage collector. I presume that you're doing a new Employee 10 times,
yes? As long as those structures are no longer referenced when you're done
passing them to native code, the fact that they're still around for a while
is completely irrelevant. That's the way garbage-collected systems almost
always work. As long as you're not running out of memory, be happy that no
time was wasted to free memory; your code is running faster because of it.

If the structure is *allocated* by native code, you'd need to either call
the appropriate native function to free it or do whatever the native library
expects you to do to free it.

Paul T.
 
G

Guest

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
 
G

Guest

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


sohail said:
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
 
G

Guest

What is MarshalStrData?


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


sohail said:
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


sohail said:
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
 
G

Guest

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


sohail said:
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
 
G

Guest

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




sohail said:
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


sohail said:
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
 
G

Guest

Thanks for your help. can you please let me know, How can I change this
bad code into good code? which works more efficientl, as I'm newbie with
interop.

Thanks

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




sohail said:
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
 
G

Guest

It's not a short newsgroup post to explain what's wrong. You need to learn
about GCHandles, as that's the only way to safely create a pointer for this
situation (well not the only, but let's stick to one method at a time).

Your struct will need to change as well, to hold 3 IntPtrs, not strings.

And you still need to determine if you must have BSTR on the native side (in
which case you'll completely have to change how you marshal them) or if you
can change them to wchar_t* types.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com




sohail said:
Thanks for your help. can you please let me know, How can I change this
bad code into good code? which works more efficientl, as I'm newbie with
interop.

Thanks

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




sohail said:
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
 
G

Guest

Thanks i'll check it

It's not a short newsgroup post to explain what's wrong. You need to learn
about GCHandles, as that's the only way to safely create a pointer for this
situation (well not the only, but let's stick to one method at a time).

Your struct will need to change as well, to hold 3 IntPtrs, not strings.

And you still need to determine if you must have BSTR on the native side (in
which case you'll completely have to change how you marshal them) or if you
can change them to wchar_t* types.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com




sohail said:
Thanks for your help. can you please let me know, How can I change this
bad code into good code? which works more efficientl, as I'm newbie with
interop.

Thanks

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top