Click here to Skip to main content
16,008,010 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: debug managed and unmanaged code Pin
jspano18-Jul-03 16:41
jspano18-Jul-03 16:41 
GeneralRe: debug managed and unmanaged code Pin
jspano18-Jul-03 8:56
jspano18-Jul-03 8:56 
GeneralRe: debug managed and unmanaged code Pin
Nathan Blomquist21-Jul-03 1:58
Nathan Blomquist21-Jul-03 1:58 
GeneralRe: debug managed and unmanaged code Pin
jspano21-Jul-03 2:05
jspano21-Jul-03 2:05 
QuestionHow To Efficiently Copy Int32 to unsigned char __gc [] Pin
T R B16-Jul-03 5:56
T R B16-Jul-03 5:56 
AnswerRe: How To Efficiently Copy Int32 to unsigned char __gc [] Pin
Italo Silveira16-Jul-03 7:00
Italo Silveira16-Jul-03 7:00 
GeneralRe: How To Efficiently Copy Int32 to unsigned char __gc [] Pin
T R B16-Jul-03 7:05
T R B16-Jul-03 7:05 
GeneralStoring a static dictionary of class instances Pin
Jeremy Osner15-Jul-03 5:36
Jeremy Osner15-Jul-03 5:36 
Something I frequently need to do in my programs, is to store at static linkage a dictionary of currently active instances of a particular class. Managed C++ makes this a little difficult; I have found two ways to do it. Both have advantages and drawbacks; I would like to get peoples feedback about which is better.

The first method is, to make the dictionary itself a managed object. For some reason I can't quite figure out, you are allowed to declare static pointers to managed types if they are declared as static members of a managed class. This makes no sense to me but is true. So take this code fragment:

__gc class A
{
static HybridDictionary *instances;
static int iInstance;

int tag;

public:
A()
{
if (instances == 0)
instances = new HybridDictionary;
tag = ++iInstance;
instances->Add(__box(tag), this);
}
void Destroy()
{
instances->Remove(__box(tag));
if (instances->Count == 0)
instances = 0;
}
static A *Lookup(int tag)
{
if (instances == 0)
return 0;
return dynamic_cast instances->Item[__box(tag)];
}
};

The big drawback to this method is, garbage collection no longer works. You have to actively call Destroy() to get an A to remove itself from the map, because the map contains a reference that will otherwise prevent the object from being destroyed.

The other method is, make the dictionary a non-managed class and use __pin to convert the instance pointer into a non-managed pointer. The main drawback here (I think) is that the documentation says not to do it. Consider the following code (assume that the InstanceMap class has been defined and takes non-managed pointers to void):

__gc class B
{
static InstanceMap instances;
static int iInstance;

int tag;

public:
B()
{
tag = ++iInstance;
B __pin *pThis = this;
instances.Add(tag, pThis);
}
~B()
{
instances.Remove(tag);
}
static A *Lookup(int tag)
{
return (B *) instances.Item(tag);
}
};

I have not actually tried this yet and now realize that I had not completely thought out the Lookup() function -- will the compiler be able to convert the void * sent back from Item() to a B *? If so this method seems way cleaner and better, and allows me to use garbage collection. Any thoughts?

Jeremy

READIN writin rhythmetic
GeneralRe: Typos Pin
Jeremy Osner15-Jul-03 6:19
Jeremy Osner15-Jul-03 6:19 
GeneralRe: Storing a static dictionary of class instances Pin
Jeremy Osner15-Jul-03 6:34
Jeremy Osner15-Jul-03 6:34 
GeneralRule of Three for Managed C++ Pin
mond_m15-Jul-03 4:54
sussmond_m15-Jul-03 4:54 
GeneralRe: Rule of Three for Managed C++ Pin
Tom Archer18-Jul-03 7:38
Tom Archer18-Jul-03 7:38 
Generaldate class problem Pin
Forever8214-Jul-03 21:55
Forever8214-Jul-03 21:55 
QuestionHOW TO MAKE A SIMPLE C SOLUTION Pin
eggie513-Jul-03 14:50
eggie513-Jul-03 14:50 
AnswerRe: HOW TO MAKE A SIMPLE C SOLUTION Pin
leppie14-Jul-03 7:22
leppie14-Jul-03 7:22 
GeneralRe: HOW TO MAKE A SIMPLE C SOLUTION Pin
eggie514-Jul-03 10:17
eggie514-Jul-03 10:17 
GeneralRe: HOW TO MAKE A SIMPLE C SOLUTION Pin
John M. Drescher15-Jul-03 5:02
John M. Drescher15-Jul-03 5:02 
GeneralManaged DLL with unmanaged MFC application Pin
stub11-Jul-03 5:17
stub11-Jul-03 5:17 
GeneralRe: Managed DLL with unmanaged MFC application Pin
Nathan Blomquist11-Jul-03 5:31
Nathan Blomquist11-Jul-03 5:31 
GeneralRe: Managed DLL with unmanaged MFC application Pin
Heath Stewart11-Jul-03 5:51
protectorHeath Stewart11-Jul-03 5:51 
GeneralRe: Managed DLL with unmanaged MFC application Pin
rahtrow11-Jul-03 8:31
rahtrow11-Jul-03 8:31 
GeneralManaged strings and buffers to nonmanaged Pin
jspano10-Jul-03 7:58
jspano10-Jul-03 7:58 
GeneralRe: Managed strings and buffers to nonmanaged Pin
Nathan Blomquist10-Jul-03 8:32
Nathan Blomquist10-Jul-03 8:32 
GeneralRe: Managed strings and buffers to nonmanaged Pin
jspano11-Jul-03 3:23
jspano11-Jul-03 3:23 
GeneralRe: Managed strings and buffers to nonmanaged Pin
Nathan Blomquist11-Jul-03 4:25
Nathan Blomquist11-Jul-03 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.