If I pin a member of a garbage collected structure, can I safely assume
that the entire struct is pinned?
I use something like that: (new whidbey syntax, but that shouldn't make
a difference):
[StructLayout(La youtKind::Seque ntial)]
public ref struct MyStruct {
Int32 memberA;
Int16 memberB;
Int32 memberC;
}
....
MyStruct^ struct = gcnew MyStruct();
void* pData = somedata_somwhe re;
//pData points to a memory location,
//that holds data not only about
//memberA but also memberB and memberC
pin_ptr<int> pMemberA = &struct->membera;
memcpy((void*)p MemberA, pData, sizeofdata);
pMemberA = nullptr;
it works but is it correct?
Would it also be correct if i hadn't specified LayoutKind::Seq uential?
Thanks
Ben 3 1256
> Would it also be correct if i hadn't specified LayoutKind::Seq uential?
Replying to myself:
Well, since memberA is not guranteed to be the first member in memory, I
suppose it wouldn't be correct to use a pointer to memberA as a pointer
to the struct.
Still, could the GC potentially shift other members around if only one
member is pinned and the LayoutKind is not specified?
I know that when you pin a member of an array, the entire array gets
pinned.
No, pinning any member will pin the entire object. That is a hard guarantee
that the CLI spec makes.
Practically in the current CLR implementation, different parts do not move
independently or get rearranged at all. Future implementations might in fact
start doing this, so you were correct to think of this possibility.
Ronald Laeremans
Visual C++ team
"Ben Schwehn" <b.*******@gmx. net> wrote in message
news:2k******** ****@uni-berlin.de... Would it also be correct if i hadn't specified LayoutKind::Seq uential? Replying to myself: Well, since memberA is not guranteed to be the first member in memory, I suppose it wouldn't be correct to use a pointer to memberA as a pointer to the struct. Still, could the GC potentially shift other members around if only one member is pinned and the LayoutKind is not specified?
I know that when you pin a member of an array, the entire array gets pinned.
> No, pinning any member will pin the entire object. That is a hard guarantee that the CLI spec makes.
Thanks!
Ben This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Wenjie |
last post by:
Hello,
I read someone posted assertions that even the (public) member
function is not static, there are probably only one copy of the
code in the executable. Then except the dependency/independency
on the lifecycle of the object, what is the significant
differences between public member functions and public static
member functions?
|
by: Jian H. Li |
last post by:
Hello,
What's the essential differences between the two ways of "class::member"
& "object.member"(or object_pointer->member)?
class C{
public:
void f() {}
int i;
};
|
by: Chris Finlayson |
last post by:
Hello:
I have a question on pinning types via 'fixed'. In "Com and .NET
interoperability" by Andrew Troelsen, he provides a simple example of
..NET -> Native interoperability:
int theVals={1,2,3,4};
MyCustomDLLWrapper.AddArray(theVals,theVals.Length)
where MyCustomDLLWrapper wraps a native (C++) .dll:
|
by: J |
last post by:
I understand how to pin pointers as they are declared, but for some
reason, I can'f figure out the syntax for predaclaring a pointer and
then pinning in afterward like you would if you wanted to embed an
unmanaged object: Assumingthat pAudioU has alread been defined
as the appriate type of pointer,
Instead of
pAudioU = new __pin AudioDriver_U; // syntax error
it seems necessary to take the extra step of creating an intermediate
|
by: Sai Kit Tong |
last post by:
I have to interface managed application with my legacy dll. I have employed
the wrapper approach but I have to deal with the asynchronous callback from
the legacy dll, which likely goes through a thread other than the initial
calling thread. I got the idea from MSDN and other articles
on using the delegate. However, for garabage collection issue, I need to pin
the delegate. Since my callback is asynchronous, I have been thinking about...
| |
by: demofo |
last post by:
Does the pinning pointer safe?since the Read function will change the
ppBuffer points to unmanaged C++ heap,how does the dot net runtime knows
they will not move the allocated memory (pointed by ppBuffer)during the
garbage collection?
class NonGCClass
{
private:
int GetBufferLength()
{
|
by: Bjoern Jonsson via .NET 247 |
last post by:
Hi all,
i've just encountered a problem with pinning. it seems as if a managed object is moved altough a pinning pointer to this object is still alive
code snippet:
ManagedObject __pin* pManagedObject = &this->managedObject;
/*lots of unmanaged memory allocation*/
|
by: Brian Victor |
last post by:
I have a situation where I have a vector<gcroot<ManagedWrapperClass*> >
whose contents I need to pass to an unmanaged function. Is there a way
to pin all the pointers in the vector for that function? I tried to
copy the vector into another vector with a pinned pointer type, but the
compiler will not allow __pin to be used in a template parameter.
Thanks!
--
Brian
|
by: Maxwell |
last post by:
Hello,
Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project
where I have a managed class reference in a unmanaged class...simple
enough. To keep things short I am for the most part attempting to do
what is this article by Nish:
http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw
I have to hook up a unmanaged callback to a managed method using IJW
NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
|
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= |
last post by:
I am very sorry to bring a topic up that has been beaten with an ugly
stick..... but...
If I want to "fix" an byte array for the life time of a program would it be
better allocating it on the unmanaged heap?
Yes I know that if I am to pin the object for long periods of time, that it
would be best to do it at the very initial start of the program so that it
prevents some fragmentation of the managed heap.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |