473,396 Members | 1,764 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,396 software developers and data experts.

pinning pointer problem

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()
{
return rand();
}
public:
int Read(unsigned char **ppBuffer)
{
const int bufferLength=GetBufferLength();
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffer,0x64,bufferLength);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBuffer;

const int bufferLength=m_pNonGC->Read(&pBuffer);

Byte byteArray[] = new Byte[bufferLength];
Marshal::Copy((IntPtr)pBuffer,byteArray,0,bufferLe ngth);

delete []pBuffer;

pBuffer=NULL;
pPinBuffer=NULL;
}
};
Nov 16 '05 #1
1 1641
demofo wrote:
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()
{
return rand();
}
public:
int Read(unsigned char **ppBuffer)
{
const int bufferLength=GetBufferLength();
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffer,0x64,bufferLength);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBuffer;

const int bufferLength=m_pNonGC->Read(&pBuffer);

Byte byteArray[] = new Byte[bufferLength];
Marshal::Copy((IntPtr)pBuffer,byteArray,0,bufferLe ngth);

delete []pBuffer;

pBuffer=NULL;
pPinBuffer=NULL;
}
};


I'm not quite sure what you're asking, but a pinning pointer pins the thing
pointed to, not the pointer used to initialize it. Above, pinning pBuffer
accomplishes nothing, as it is an unmanaged pointer. There is no problem
with your use of your Read function. It simply stores an unsigned char* in
another unsigned char*. If you were dealing with __gc pointers all around,
it would be no problem to overwrite one with the other; the pinning pointer
would be unaffected by this.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2

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

Similar topics

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...
4
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...
3
by: Ben Schwehn | last post by:
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): ...
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*...
9
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...
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...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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
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...
0
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
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...

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.