473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Pinning Vectors

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

Nov 17 '05 #1
9 1515
__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<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
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<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
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<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[i] = items[i];
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
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_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[i]);
GetItem_Unmanaged(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
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...
3
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...
1
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...
7
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
0
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*...
3
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...
5
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...
2
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....
12
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.