473,657 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create Managed Object in Unmanaged class function

All samples in Micoroft just mention that if a managed
type is used as a member in nongc class, for example:
gcroot<String*> is used to define a String managed member
variable. What if the managed object in unmanaged class is
just defined in a member function instead of a member
variable. For example:

// managed imports
#using <mscorlib.dll >
using namespace System;
using namespace System::Runtime ::InteropServic es;

// unmanaged C++ class

class CSimple
{
public:
void Foo();
};

CSimple::Foo()
{
String *test = new String(S"FOO Test");
// Or should test be defined as gcroot<String*>
String nospace = test->Replace(" ", "_");

// Or should nospace be defined as gcroot<String*>
too????

}

it compiles fine without gcroot<String*> , but is it safe
for GC collector, should I define nospace as
gcroot<String*>

Thanks very much for your help!

Kevin
Nov 17 '05 #1
1 4447
Kevin wrote:
All samples in Micoroft just mention that if a managed
type is used as a member in nongc class, for example:
gcroot<String*> is used to define a String managed member
variable. What if the managed object in unmanaged class is
just defined in a member function instead of a member
variable. For example:

// managed imports
#using <mscorlib.dll >
using namespace System;
using namespace System::Runtime ::InteropServic es;

// unmanaged C++ class

class CSimple
{
public:
void Foo();
};

CSimple::Foo()
{
String *test = new String(S"FOO Test");
// Or should test be defined as gcroot<String*>
No, CLR can track this and you don't need a gcroot<>
String nospace = test->Replace(" ", "_");

// Or should nospace be defined as gcroot<String*>
too????
No, see above.

}

it compiles fine without gcroot<String*> , but is it safe
for GC collector, should I define nospace as
gcroot<String*>


No, you are fine.

The reason that gcroot<> is needed for members of a __nogc class is that the
the destruction of an object of a __nogc class, and therefore the release of
a GC pointer in an embedded member, can not normally be tracked by the CLR.
Using gcroot gives the CLR a way to track it.
Nov 17 '05 #2

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

Similar topics

5
3626
by: Chris Kiechel | last post by:
I am writing a .NET Windows application and it needs to perform DDE calls to a legacy system. I created a C++ unmanaged class that performs the actual DDE connection and communication. However, I need .NET to instantiate this object and somehow allow the this object to callback to .NET. So, my question is: 1. How can a managed class instantiate and unmanaged class?
7
1857
by: Lev | last post by:
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...
2
2408
by: Brett Styles | last post by:
Hi Guys, I am trying to access a class in an unmanaged dll. I have created a wrapper managed class to access the functions I need but no matter what I try from the MSDN samples I can not get it to work with my code. I have a VB Net front end which need the access the unmanaged functions. The wrapper I wrote can be accessed but the wrapper will not compile when I refer to the unmanaged class. I have tried using header file for definitions...
3
2766
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple question.
13
5040
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to use that dll from pure unmanaged code (for example inherit from classes in that dll). Is something like that possible. I heared something called ManWarp tried that approach. If it is possible, how can I do that. Maybe there is a small litttle
25
3008
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
3
14492
by: vijay.gandhi | last post by:
Hi, I am trying to convert some unmanaged code (C++) to managed code (using C++/CLI). 1) One of the functions used returns a void* which I need to cast into a handle of a managed object. Can somebody please tell me how. Actually, the function that returns a void* is a part of a MFC class - CPtrList, used in my unmanaged code. I was searching of its equivalent
3
4702
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting enhancement which asynchronly
9
3547
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback function I need to cast this unmnaged pointer that I have received from 3rd party back into the...
0
8315
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
8829
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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...
0
7341
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...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
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
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
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.