473,499 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do we have to keep the object alive while doing pinvoke?

Hello,

After reading a few articles,
http://blogs.gotdotnet.com/cbrumme/P...0-f0fa27ab6cc0
http://blogs.gotdotnet.com/anathan/c...7-1b6c677214cb

i have the following question:

__gc class C
{
C(){ ptr = new Unmanaged(); }
~C(){ delete ptr; ptr = 0; }

void DoWork()
{

//Do we have to HandleRef ref(this, IntPtr(ptr));
ptr->UnmnagedCall();
//or call GC::KeepAlive(this)
}

Unmanaged __nogc* ptr;
};

Do we have to do
HandleRef ref(this, IntPtr(ptr)) or GC::KeepAlive?
No one seems to talk about this. Does MC++ take care of
this problem?

Thanx for any input
Nov 16 '05 #1
1 1840
Hello
The rules are very simple .

Consider a simple sync unmanaged call from a managed function.
What kind of data are you passing to the unmanaged function? Nothing ? You
have nothing to worrry about !
If you are only passing addresses of any managed data like a string
pointer, a garbage collection cycle can change them(remember the GC will
move the objected) and so using them from unmanaged functions is not safe.
That is why you will pin the variable. see code below

#using <mscorlib.dll>
using namespace System;
#include <vcclr.h>
#include <stdio.h>
int main()
{
String* s = "hello";
const __wchar_t __pin* pinned_ptr = PtrToStringChars(s);
//s will be alive till pinned_ptr is in scope. Sufficient for
Synchronous functions
wprintf(pinned_ptr); //unmanaged call

}

On the other hand , if you are making a async call, you want to keep a
Pinned GCHandle alive till the async call is complete. This is for
unmanaged functions manipulating the managed heap directly and so the need
to not move them .

THis is using IJW in MC++ . If you are using PINVOKE like in C# , the
pinvoke layer will take care of this for sync calls.
http://blogs.gotdotnet.com/cbrumme/d...05-06T00:00:00
also has some data.

--------------------
From: cp*****@yahoo.com (cppdev)
Newsgroups: microsoft.public.dotnet.languages.vc
Subject: Do we have to keep the object alive while doing pinvoke?
Date: 25 Jul 2003 11:56:07 -0700
Organization: http://groups.google.com/
Lines: 30
Message-ID: <fc**************************@posting.google.com >
NNTP-Posting-Host: 68.164.38.125
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1059159368 24658 127.0.0.1 (25 Jul 2003 18:56:08 GMT)X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: 25 Jul 2003 18:56:08 GMT
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-06!sn-
xit-09!supernews.com!postnews1.google.com!not-for-mailXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26553
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hello,

After reading a few articles,
http://blogs.gotdotnet.com/cbrumme/P...71-48b9-b360-f 0fa27ab6cc0http://blogs.gotdotnet.com/anathan/c...6290-4500-a8c7 -1b6c677214cb
i have the following question:

__gc class C
{
C(){ ptr = new Unmanaged(); }
~C(){ delete ptr; ptr = 0; }

void DoWork()
{

//Do we have to HandleRef ref(this, IntPtr(ptr));
ptr->UnmnagedCall();
//or call GC::KeepAlive(this)
}

Unmanaged __nogc* ptr;
};

Do we have to do
HandleRef ref(this, IntPtr(ptr)) or GC::KeepAlive?
No one seems to talk about this. Does MC++ take care of
this problem?

Thanx for any input


Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1042
by: xeoicq | last post by:
I wrote a COM server in Python where all the clients use the same global object(test_obj). So far it works, but when the last client is closed the Python COM enviornment is closed and the global...
1
2802
by: Robin Tucker | last post by:
Just a quick question about connection management. My application will never need more than 1 or 2 connections about at any given time. Also, I do not expect many users to be connected at any...
0
1810
by: Gil Strauss | last post by:
When using .net framework HttpWebRequest object, how can we set the connection header to "keep-alive" Acording to Microsoft ducomentation, setting the keepAlive property to true, should do the job,...
2
7270
by: David Rasmussen | last post by:
Someone once asked about this an got no answer: ...
0
921
by: Dody Suria Wijaya | last post by:
I'm looking for a RPC library in Python that can keep its TCP connection alive for the duration of a user's login session. The reason is, each user login to the application server will impose a...
19
2409
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
6
5646
by: laredotornado | last post by:
Hi, When a user logs into our site, we create a session variable to denote the session is active and another to denote who is logged in. Once the user closes the browser and re-opens it, the...
1
1284
by: Laura D. | last post by:
website transactions? Hi all. I've working on http://www.picturesoffood.org about pictures of food. Yummy! Anyway, I upload all the files using FTP, but then I use php to process them, either the...
0
1490
by: Ramchandar | last post by:
In my aspx page i want the lot of informations to be filled by the user. But wen the user takes some break to fill the form n resumes after some time the page gets expired. So to keep the session...
0
7178
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
7223
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...
1
6899
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...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.