473,785 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pinning Vectors

I have a situation where I have a vector<gcroot<M anagedWrapperCl ass*> >
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

Nov 17 '05 #1
9 1541
__gc class X
{

};

__nogc class W
{
vector<gcroot<X *> > arr;
public:
W()
{
X __pin* p = new X;
arr.push_back(p );
}
};

is this OK?

Ismail

Nov 17 '05 #2
At 9:37 PM on May 26, 2005, ismailp wrote:
X __pin* p = new X;
arr.push_back(p ); [snip]is this OK?


I'm not sure it is. My understanding of pinned pointers is that the
__pin keyword applies to the variable in the local scope and the pin is
released when that variable goes out of scope. So it seems to me that
push_back in the above code pushes a pointer that is implicitly
converted to a non-pinned pointer, then the pinned pointer goes out of
scope, releasing the pin.

If my understanding is off, I would be happily corrected.

--
Brian

Nov 17 '05 #3
ismailp wrote:
__gc class X
{

};

__nogc class W
{
vector<gcroot<X *> > arr;
public:
W()
{
X __pin* p = new X;
arr.push_back(p );
}
};

is this OK?

Ismail


No it's not. The pin is released right away.

I think you have to duplicate the system call that __pin is doing
automatically in the background. I don't know which one it is. In other
words, you need a pinning mechanism that's not scope limited.

Tom
Nov 17 '05 #4
Brian Victor wrote:
I have a situation where I have a vector<gcroot<M anagedWrapperCl ass*> >
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!


From the VC++ 2005 help:
<quote>
Pinning a sub-object defined in a managed object has the effect of
pinning the entire object. For example, if any element of an array is
pinned, then the whole array is also pinned.

// pin_ptr_array.c pp
// compile with: /clr
#include <stdio.h>
using namespace System;

int main() {
array<Byte>^ arr = gcnew array<Byte>(4);
arr[0] = 'C';
arr[1] = '+';
arr[2] = '+';
arr[3] = '0';
pin_ptr<Byte> p = &arr[1]; // entire array is now pinned
unsigned char * cp = p;

printf_s("%s\n" , cp); // bytes pointed at by cp
// will not move during call
}
</quote>

I'm not sure if this is true for VC++ 2003 (MC++ syntax), but I think it is.

Is it possible that you change your design and instead of storing an
unmanaged STL vector<gcroot<M anagedWrapperCl ass*> >, you could be
storing your managed classes in a managed array. I think it would be
doable, and gcroot the entire managed array. That way if you pin the
first item in the array, the entire array will be pinned. Just an idea.
Maybe someone else has a better recommendation.

On the other hand, I would be cautious about pinning a managed class.
Are you sure that an unmanaged code can interpret the internals of a
managed class? Is your managed class byte-compatible with an unmanaged
data structure? How are the member variables aligned inside? I may be
wrong, but I've only pinned managed byte arrays and integer arrays,
because I know they're byte-compatible with their unmanaged counterparts.

Tom
Nov 17 '05 #5
Pinning pointers are like native pointers. so they are independent from
scope.

the container is inside an unmanaged object. therefore, you can't use
managed arrays. you have to use vector<gcroot<T > >.

what's that byte compatibility? of course, unmanaged code is not CLI
code. when you pin a pointer, you can access it from native code. yes,
we have a GC heap and a native heap, in managed C++. but both of those
heaps are accessible by anyone from the process. except, you cannot
treat managed pointers like native pointers - unmanaged pointers do not
move. this is why we pin them. therefore, you can access "any pinned
pointer or a native pointer points somewhere in heap" from "anywhere"
in your code.

did you try that code?

Ismail

Nov 17 '05 #6
ismailp wrote:
Pinning pointers are like native pointers. so they are independent from
scope.
I meant the fact of pinning is scoped. From the help:
"You should not convert your pinning pointer to an unmanaged pointer and
continue using this unmanaged pointer after the object is no longer
pinned (after the pinning pointer goes out of scope)."

Once your pinned pointer goes out of scope, the pin is automatically
released.

He needs to pin all managed objects inside an unmanaged vector, and keep
them all pinned during a short period of time (while he's calling an
unmanaged function). Pinning is a scoped operation, and therefore it
can't be done in a loop. How do you pin all items in a loop, and keep
all of them pinned at once? My best bet would be to copy the
vector<gcroot<M anagedWrapper*> > to a managed array, and pin the first
item of it. However, I'm no sure if that would work. What happens if you
pin the first item of a managed array containing managed classes? Would
it pin all its members, or just the array itself?
did you try that code?


Your code? The problem is that you're pinning an item, and returning
from the function (constructor), which releases the pin. Am I right?

Tom
Nov 17 '05 #7
This is what I wanted to recommend:

__gc class Managed
{
public:
int x;
Managed(int value) : x(value) { }
};

__nogc class Unmanaged
{
private:
std::vector<gcr oot<Managed*> > items;
public:
Unmanaged()
{
items.push_back (__gc new Managed(10));
items.push_back (__gc new Managed(20));
}
void Do()
{
Managed* managed_items[] = __gc new Managed*[items.size()];
for(unsigned i = 0; i < items.size(); ++i)
managed_items[i] = items[i];
Managed* __pin* p = &managed_ite ms[0];
// [...]
}
};

However, I have a gut feeling that pinning the managed_items is not
going to pin all objects in the array, only the array itself.

In the worst case, it's impossible to pin all objects at once, and they
have to be pinned one at a time.

The original poster had a very tough question. :-)

Tom
Nov 17 '05 #8
At 2:48 PM on May 27, 2005, Tamas Demjen wrote:
The original poster had a very tough question. :-)


Thanks. ;)

I confess I still have a certain insecurity when it comes to pinning.
I learned the hard way that I needed to pin some data when using it in
unmanaged code, but I'm still not 100% sure when those cases are. I'll
give your code a shot and see if it seems to work.

On that note, do you (or anyone) know if there's a way to force the GC
to be hyperactive? I.e., to make it move memory as often as possible
so that any pinning bugs are exposed during testing rather than in the
field?

--
Brian

Nov 17 '05 #9
yes, i have seen that now. Ok, then it means __pin doesn't mean what I
understand from that. so when constructor returns, vector will be
storing gc holes. may be they will be valid, but until next garbage
collection. therefore, storing those pointers in a managed array is a
better idea. so he has to call an unmanaged function, use pinned
pointer in it, and then code has to forget about pinned pointer. that
could be the only way. so actually there may not be anything need to be
pinned.

#pragma unmanaged
class Unmngd
{
public:
void f()
{

}
};

void GetItem_Unmanag ed(Unmngd* p)
{
// use p here
}
#pragma managed

__gc class ManagedWrapper
{
Unmngd* p;
public:
ManagedWrapper( )
{
p = new Unmngd;
}
~ManagedWrapper ()
{
if (p)
delete p;
}

void f()
{
p->f();
}
Unmngd* GetNativePtr()
{
return p;
}
};

__gc class X
{
ArrayList *m_arrObjects;
public:
void Add(ManagedWrap per* p)
{
m_arrObjects->Add(p);
}
void GetItem(int i)
{
ManagedWrapper * pObj =
__try_cast<Mana gedWrapper*>(m_ arrObjects->Item[i]);
GetItem_Unmanag ed(pObj->GetNativePtr() );
}
};
Is this OK?

Ismail

Nov 17 '05 #10

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

Similar topics

2
1548
by: Henrique | last post by:
Hi Kind of newbie question: We have a DLL with a function with a prototype like void mult( int mtxhandle, double* x, double* y, int size ); where "size" is the size of both x and y vectors. What should be the correct sintax to define its respective DllImport wrapper in order to use it (in C#)
3
1543
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
1
1657
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() {
7
1321
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
0
980
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*/
3
2216
by: Hexar Anderson | last post by:
I have two questions: a) From documentation located at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_7_7.asp, it says, "Pinning a sub-object defined in a managed object has the effect of pinning the entire object. For example, if any element of an array is pinned, then the whole array is also pinned. There are no extensions to the language for declaring a pinned array. To pin an...
5
3418
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...
2
7909
by: steve | last post by:
Hi all, I want to understand more about how the pinvoke pinning process works. I'm writing some code that calls DeviceIoControl. DeviceIoControl provides a generic interface to device drivers. Its signature is deliberately open-ended so that it can be highly generic. Refer SDK for more. I want to access the drive geommetry of an SD-Card via
12
12557
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley retreiving the data. Presently all of the c++ code is still in .NET 1.1, so we're using a _nogc bridge class wrapped in a _gc c++ class in order to facilitate this interop. But we've converted everything not c++ to .NET 2.0 and would love to
0
9480
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,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 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...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 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...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
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 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...
1
4046
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.