473,396 Members | 1,879 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.

Ensuring unmanaged pointers held by managed classes

Lev
Hi,

I have an unmanaged pointer to a class that I want to hold in a managed
class. I pass the pointer (from unmanaged code) in the constructor of the
managed class, and at that point it has value. It is stored in a member
variable in my managed class declared as MyObj __nogc* pMyObj. When I try to
retrieve it later, using a function declared as MyObj __nogc* GetObj, the
pointer has been corrupted and shows up as <undefined value> when inspecting
it from the debugger. How do I prevent that?

Thanks,

Lev
Nov 17 '05 #1
7 1847
Does the object definitely still exist? If the object the pointer points to
has ceased to be, then the pointer you're looking at will be invalid.
Posting a bit more code might help - a cut-down test case or something. I
don't pass in unmanaged pointers to functions - wherever I've had to mix
managed/unmanaged, it's always been possible for the managed class to own
the unmanaged. Let me know if you figure out what the problem is.

Steve

"Lev" <bo*****@hotmail.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hi,

I have an unmanaged pointer to a class that I want to hold in a managed
class. I pass the pointer (from unmanaged code) in the constructor of the
managed class, and at that point it has value. It is stored in a member
variable in my managed class declared as MyObj __nogc* pMyObj. When I try to retrieve it later, using a function declared as MyObj __nogc* GetObj, the
pointer has been corrupted and shows up as <undefined value> when inspecting it from the debugger. How do I prevent that?

Thanks,

Lev

Nov 17 '05 #2
Lev
Hi Steve,

Yes, I am positive that the object pointed to still exists. The object
pointed to is created even before the managed class is created and deleted
long after the managed class has ceased to exist. I am now trying further
investigations into the modification of the pointer... Any ideas are much
appreciated :-)

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:eD*************@tk2msftngp13.phx.gbl...
Does the object definitely still exist? If the object the pointer points to has ceased to be, then the pointer you're looking at will be invalid.
Posting a bit more code might help - a cut-down test case or something. I
don't pass in unmanaged pointers to functions - wherever I've had to mix
managed/unmanaged, it's always been possible for the managed class to own
the unmanaged. Let me know if you figure out what the problem is.

Steve

"Lev" <bo*****@hotmail.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hi,

I have an unmanaged pointer to a class that I want to hold in a managed
class. I pass the pointer (from unmanaged code) in the constructor of the managed class, and at that point it has value. It is stored in a member
variable in my managed class declared as MyObj __nogc* pMyObj. When I try
to
retrieve it later, using a function declared as MyObj __nogc* GetObj,

the pointer has been corrupted and shows up as <undefined value> when

inspecting
it from the debugger. How do I prevent that?

Thanks,

Lev


Nov 17 '05 #3
Thought I'd check :-)

When are you looking at it in the debugger? Are you sure the constructor's
been entered when you're viewing it? None of the stuff passed in to a
function is defined immediately when the function is entered; the first
debugger 'step' takes it into the function. If you try and use the pointer
inside the managed class, do you get an error?

Steve
Hi Steve,

Yes, I am positive that the object pointed to still exists. The object
pointed to is created even before the managed class is created and deleted
long after the managed class has ceased to exist. I am now trying further
investigations into the modification of the pointer... Any ideas are much
appreciated :-)

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:eD*************@tk2msftngp13.phx.gbl...
Does the object definitely still exist? If the object the pointer points

to
has ceased to be, then the pointer you're looking at will be invalid.
Posting a bit more code might help - a cut-down test case or something. I
don't pass in unmanaged pointers to functions - wherever I've had to mix
managed/unmanaged, it's always been possible for the managed class to own the unmanaged. Let me know if you figure out what the problem is.

Steve

"Lev" <bo*****@hotmail.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
Hi,

I have an unmanaged pointer to a class that I want to hold in a managed class. I pass the pointer (from unmanaged code) in the constructor of

the managed class, and at that point it has value. It is stored in a member variable in my managed class declared as MyObj __nogc* pMyObj. When I try
to
retrieve it later, using a function declared as MyObj __nogc* GetObj,

the pointer has been corrupted and shows up as <undefined value> when

inspecting
it from the debugger. How do I prevent that?

Thanks,

Lev



Nov 17 '05 #4
Lev
Yes, I am quite confident that the constructor has been entered. I assign
the pointer to a member variable as the last step in the constructor.
However, when I tried to execute a method on the pointer (it is an interface
pointer), I got a System.OutOfMemoryException... That leaves me at a loss...
"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thought I'd check :-)

When are you looking at it in the debugger? Are you sure the constructor's
been entered when you're viewing it? None of the stuff passed in to a
function is defined immediately when the function is entered; the first
debugger 'step' takes it into the function. If you try and use the pointer
inside the managed class, do you get an error?

Steve
Hi Steve,

Yes, I am positive that the object pointed to still exists. The object
pointed to is created even before the managed class is created and deleted
long after the managed class has ceased to exist. I am now trying further investigations into the modification of the pointer... Any ideas are much appreciated :-)

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:eD*************@tk2msftngp13.phx.gbl...
Does the object definitely still exist? If the object the pointer points
to
has ceased to be, then the pointer you're looking at will be invalid.
Posting a bit more code might help - a cut-down test case or
something.
I don't pass in unmanaged pointers to functions - wherever I've had to
mix managed/unmanaged, it's always been possible for the managed class to

own the unmanaged. Let me know if you figure out what the problem is.

Steve

"Lev" <bo*****@hotmail.com> wrote in message
news:ON**************@tk2msftngp13.phx.gbl...
> Hi,
>
> I have an unmanaged pointer to a class that I want to hold in a managed > class. I pass the pointer (from unmanaged code) in the constructor
of the
> managed class, and at that point it has value. It is stored in a member > variable in my managed class declared as MyObj __nogc* pMyObj. When

I try
to
> retrieve it later, using a function declared as MyObj __nogc*
GetObj, the
> pointer has been corrupted and shows up as <undefined value> when
inspecting
> it from the debugger. How do I prevent that?
>
> Thanks,
>
> Lev
>
>



Nov 17 '05 #5
Lev
Ok, did some further investigations. If I call a method on the passed
interface pointer from the constructor, all is well. This leads me to
believe that the pointer stored internally in the managed class gets
corrupted or gc'ed eventhough it is declared as __nogc...

"Lev" <boazlev NOSPAM at h0tmail dot com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Yes, I am quite confident that the constructor has been entered. I assign
the pointer to a member variable as the last step in the constructor.
However, when I tried to execute a method on the pointer (it is an interface pointer), I got a System.OutOfMemoryException... That leaves me at a loss...

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thought I'd check :-)

When are you looking at it in the debugger? Are you sure the constructor's
been entered when you're viewing it? None of the stuff passed in to a
function is defined immediately when the function is entered; the first
debugger 'step' takes it into the function. If you try and use the pointer inside the managed class, do you get an error?

Steve
Hi Steve,

Yes, I am positive that the object pointed to still exists. The object pointed to is created even before the managed class is created and deleted long after the managed class has ceased to exist. I am now trying further investigations into the modification of the pointer... Any ideas are much appreciated :-)

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:eD*************@tk2msftngp13.phx.gbl...
> Does the object definitely still exist? If the object the pointer points to
> has ceased to be, then the pointer you're looking at will be invalid. > Posting a bit more code might help - a cut-down test case or something.
I
> don't pass in unmanaged pointers to functions - wherever I've had to
mix > managed/unmanaged, it's always been possible for the managed class
to own
> the unmanaged. Let me know if you figure out what the problem is.
>
> Steve
>
> "Lev" <bo*****@hotmail.com> wrote in message
> news:ON**************@tk2msftngp13.phx.gbl...
> > Hi,
> >
> > I have an unmanaged pointer to a class that I want to hold in a

managed
> > class. I pass the pointer (from unmanaged code) in the constructor

of the
> > managed class, and at that point it has value. It is stored in a

member
> > variable in my managed class declared as MyObj __nogc* pMyObj.
When I try
> to
> > retrieve it later, using a function declared as MyObj __nogc* GetObj, the
> > pointer has been corrupted and shows up as <undefined value> when
> inspecting
> > it from the debugger. How do I prevent that?
> >
> > Thanks,
> >
> > Lev
> >
> >
>
>



Nov 17 '05 #6
Yes, although I'm sure it shouldn't. Try removing the __nogc bit, see if
that makes a difference, and try writing a managed wrapper for it - store it
in a simple managed class and store that in your existing managed class. It
really shouldn't be affected by the garbage collector. What's the
constructor argument? Is it exactly the same as the stored pointer def (ie
not a base class or anything)?

Steve

"Lev" <boazlev NOSPAM at h0tmail dot com> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl...
Ok, did some further investigations. If I call a method on the passed
interface pointer from the constructor, all is well. This leads me to
believe that the pointer stored internally in the managed class gets
corrupted or gc'ed eventhough it is declared as __nogc...

"Lev" <boazlev NOSPAM at h0tmail dot com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Yes, I am quite confident that the constructor has been entered. I assign
the pointer to a member variable as the last step in the constructor.
However, when I tried to execute a method on the pointer (it is an

interface
pointer), I got a System.OutOfMemoryException... That leaves me at a

loss...


"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thought I'd check :-)

When are you looking at it in the debugger? Are you sure the constructor's been entered when you're viewing it? None of the stuff passed in to a
function is defined immediately when the function is entered; the first debugger 'step' takes it into the function. If you try and use the pointer inside the managed class, do you get an error?

Steve

> Hi Steve,
>
> Yes, I am positive that the object pointed to still exists. The object > pointed to is created even before the managed class is created and

deleted
> long after the managed class has ceased to exist. I am now trying

further
> investigations into the modification of the pointer... Any ideas are

much
> appreciated :-)
>
> "Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
> news:eD*************@tk2msftngp13.phx.gbl...
> > Does the object definitely still exist? If the object the pointer

points
> to
> > has ceased to be, then the pointer you're looking at will be invalid. > > Posting a bit more code might help - a cut-down test case or

something.
I
> > don't pass in unmanaged pointers to functions - wherever I've had to mix
> > managed/unmanaged, it's always been possible for the managed class to own
> > the unmanaged. Let me know if you figure out what the problem is.
> >
> > Steve
> >
> > "Lev" <bo*****@hotmail.com> wrote in message
> > news:ON**************@tk2msftngp13.phx.gbl...
> > > Hi,
> > >
> > > I have an unmanaged pointer to a class that I want to hold in a
managed
> > > class. I pass the pointer (from unmanaged code) in the
constructor of
> the
> > > managed class, and at that point it has value. It is stored in a
member
> > > variable in my managed class declared as MyObj __nogc* pMyObj.

When
I
> try
> > to
> > > retrieve it later, using a function declared as MyObj __nogc*

GetObj,
> the
> > > pointer has been corrupted and shows up as <undefined value>

when > > inspecting
> > > it from the debugger. How do I prevent that?
> > >
> > > Thanks,
> > >
> > > Lev
> > >
> > >
> >
> >
>
>



Nov 17 '05 #7
Lev
Putting the pointer in a wrapper does not seem to help at all. Still getting
the NullReferenceException due to the pointer going into the undefined
state...

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:uz*************@TK2MSFTNGP10.phx.gbl...
Yes, although I'm sure it shouldn't. Try removing the __nogc bit, see if
that makes a difference, and try writing a managed wrapper for it - store it in a simple managed class and store that in your existing managed class. It really shouldn't be affected by the garbage collector. What's the
constructor argument? Is it exactly the same as the stored pointer def (ie
not a base class or anything)?

Steve

"Lev" <boazlev NOSPAM at h0tmail dot com> wrote in message
news:e$**************@TK2MSFTNGP10.phx.gbl...
Ok, did some further investigations. If I call a method on the passed
interface pointer from the constructor, all is well. This leads me to
believe that the pointer stored internally in the managed class gets
corrupted or gc'ed eventhough it is declared as __nogc...

"Lev" <boazlev NOSPAM at h0tmail dot com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Yes, I am quite confident that the constructor has been entered. I assign the pointer to a member variable as the last step in the constructor.
However, when I tried to execute a method on the pointer (it is an interface
pointer), I got a System.OutOfMemoryException... That leaves me at a

loss...


"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> Thought I'd check :-)
>
> When are you looking at it in the debugger? Are you sure the

constructor's
> been entered when you're viewing it? None of the stuff passed in to a > function is defined immediately when the function is entered; the first > debugger 'step' takes it into the function. If you try and use the

pointer
> inside the managed class, do you get an error?
>
> Steve
>
> > Hi Steve,
> >
> > Yes, I am positive that the object pointed to still exists. The

object
> > pointed to is created even before the managed class is created and
deleted
> > long after the managed class has ceased to exist. I am now trying
further
> > investigations into the modification of the pointer... Any ideas are much
> > appreciated :-)
> >
> > "Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message > > news:eD*************@tk2msftngp13.phx.gbl...
> > > Does the object definitely still exist? If the object the pointer points
> > to
> > > has ceased to be, then the pointer you're looking at will be

invalid.
> > > Posting a bit more code might help - a cut-down test case or
something.
> I
> > > don't pass in unmanaged pointers to functions - wherever I've had to
mix
> > > managed/unmanaged, it's always been possible for the managed
class
to
> own
> > > the unmanaged. Let me know if you figure out what the problem

is. > > >
> > > Steve
> > >
> > > "Lev" <bo*****@hotmail.com> wrote in message
> > > news:ON**************@tk2msftngp13.phx.gbl...
> > > > Hi,
> > > >
> > > > I have an unmanaged pointer to a class that I want to hold in a > managed
> > > > class. I pass the pointer (from unmanaged code) in the

constructor of
> > the
> > > > managed class, and at that point it has value. It is stored in a > member
> > > > variable in my managed class declared as MyObj __nogc* pMyObj.

When
I
> > try
> > > to
> > > > retrieve it later, using a function declared as MyObj __nogc*
GetObj,
> > the
> > > > pointer has been corrupted and shows up as <undefined value> when > > > inspecting
> > > > it from the debugger. How do I prevent that?
> > > >
> > > > Thanks,
> > > >
> > > > Lev
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #8

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
2
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
2
by: The unProfessional | last post by:
In my current project (my first project using vc w/ managed extensions), I'm directly #using <mscorlib.dll>, so it's necessary for me to use the __nogc and __gc constructs when defining classes or...
1
by: Sparhawk | last post by:
Hi, my company is going to migrate a large VC++ application to .NET to make use of Windows Forms (the old class library is not updated any more). We are not planning to migrate the rest of the...
6
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone...
4
by: Maxwell | last post by:
Hello, Newbie question here for disposing of unmanaged resources in MC++.NET. I have a managed VS.NET 2003 MC++ wrapper class that wraps a unmanaged C++ dll. What I am trying to figure out is...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
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,...
0
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...
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
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...
0
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,...

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.