Pinning Vectors

  • Thread starter Thread starter Brian Victor
  • Start date Start date
B

Brian Victor

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!
 
__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
 
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.
 
ismailp said:
__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
 
Brian said:
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!

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.cpp
// 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<ManagedWrapperClass*> >, 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
 
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
 
ismailp said:
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<ManagedWrapper*> > 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
 
This is what I wanted to recommend:

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

__nogc class Unmanaged
{
private:
std::vector<gcroot<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 = items;
Managed* __pin* p = &managed_items[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
 
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?
 
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_Unmanaged(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(ManagedWrapper* p)
{
m_arrObjects->Add(p);
}
void GetItem(int i)
{
ManagedWrapper * pObj =
__try_cast<ManagedWrapper*>(m_arrObjects->Item);
GetItem_Unmanaged(pObj->GetNativePtr());
}
};


Is this OK?

Ismail
 
Back
Top