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

Home Posts Topics Members FAQ

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=Ge tBufferLength() ;
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffe r,0x64,bufferLe ngth);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBu ffer;

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

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

delete []pBuffer;

pBuffer=NULL;
pPinBuffer=NULL ;
}
};
Nov 16 '05 #1
1 1657
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=Ge tBufferLength() ;
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffe r,0x64,bufferLe ngth);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBu ffer;

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

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

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
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
4
2403
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...
3
1257
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): public ref struct MyStruct {
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*/
9
1542
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
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...
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
9645
marktang
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...
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
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
10090
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
9949
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
7499
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
4050
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
3
2879
bsmnconsultancy
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...

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.