Click here to Skip to main content
15,909,445 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Datatype conversion problems Pin
So and So7-Jul-04 13:10
So and So7-Jul-04 13:10 
GeneralRe: Datatype conversion problems Pin
palbano8-Jul-04 12:25
palbano8-Jul-04 12:25 
GeneralCommand Pin
Anonymous4-Jul-04 20:44
Anonymous4-Jul-04 20:44 
GeneralTrying to build a DLL but only getting an EXE Pin
reznod4-Jul-04 13:29
reznod4-Jul-04 13:29 
GeneralRe: Trying to build a DLL but only getting an EXE Pin
EnemyMaker4-Jul-04 22:02
EnemyMaker4-Jul-04 22:02 
GeneralRe: Trying to build a DLL but only getting an EXE Pin
reznod5-Jul-04 4:55
reznod5-Jul-04 4:55 
GeneralSaving BMP/JPEG from Stream Pin
Emiliano2-Jul-04 6:47
Emiliano2-Jul-04 6:47 
GeneralPinnig Pointers Pin
EnemyMaker1-Jul-04 3:05
EnemyMaker1-Jul-04 3:05 
Hello,

I'm confusing with pinning pointers. In the language specification of MC++ you can read about pinning pointers:
"Pinning a sub-object defined in a managed object has the effect of pinning the entire object."

So I tried to find out if this really works, because I want be sure of it when I am using pinning pointers to convert a managed char[] char c _gc[] to an unmanaged null-terminated-char pointer.
Because a char array wouln't show, when it is destroyed, I wrote a class 'O', which does so:
__gc class O
{
    int m_i;
public:
    O(int i) : m_i(i)
    { 
        Console::WriteLine(String::Concat("constructor O ", m_i.ToString()));
    }
    ~O()
    {
    	Console::WriteLine(String::Concat("destructor O ", m_i.ToString()));
    	m_i=0;
    }
};


Then I wrote a class 'C', containing an array of 'O' which will be pinned. And this should pin the whole object.
'C' shows when it is created or destroeyed, too.
__gc class C
{
public:
    O *o[];
    
    C()
    {
        Console::WriteLine("contructor C");
        o = new O* [4];
        for(int i=0; i<4; i++)
            o[i] = new O(i+1);		
    }
    ~C()
    {
        Console::WriteLine("destructor C");
    }
};


And now the testprog. I create an instance of 'C' an pin an element of its member 'o'.
After that I remove the reference to 'c' and invoke the GC. Non of the allocated objects (the 'C' - instance itself or the array of 'O') shold be destroued.
Because I pinned a subobject of the array - so the array mus be pinned, and the array is a (now pinned) subobject of the 'C' - Class - so 'c' must be pinned, too ..
int main()
{
    C *c = new C();

    // pining the first element of 'C::o' should pin the entire object 'c'
    Console::WriteLine("O __pin*p = c->o[0]");
    O __pin*p = c->o[0];
		
    // removing the reference to 'c'
    Console::WriteLine("c = 0");
    c = 0;

    // object 'c' should not be collected:
    GC::Collect();
    GC::WaitForPendingFinalizers();

    // removing the reference to 'p'
    Console::WriteLine("p = 0");
    p = 0;

    // object 'c' should not be collected (including all members):
    GC::Collect();
    GC::WaitForPendingFinalizers();

    Console::WriteLine("END _ OF _ CODE");

    return 0;
}


I compiled with VS.NET 2003 an got the following output:
contructor C
constructor O 1
constructor O 2
constructor O 3
constructor O 4
O __pin*p = c->o[0]
c = 0
destructor O 4
destructor C
destructor O 3
destructor O 2
p = 0
destructor O 1
END _ OF _ CODE

This shows, that any element was destroyed, except the element of the 'O'-array, I was pointing to. But this is not pinning! :-/
At least the whole array of 'O' should still remain after GC.
I wonder if I misunderstud anything related to pinning or if I did something wrong ...

By the way, I tested pining before with a common Int32 - member variable of an __gc class (let me call it 'G'). After pinning this Int32-member and removing the reference to the containing object (the instance of 'G'), the whole object still remains after GC. So it seems pinning works when pinning a __value - type.
Is it possible that my problem causes in the same behavior why I shouldn't use the operator == to check two System::String for Equality?
(see http://www.winterdom.com/mcppfaq/archives/000124.html[^])
System::String *str1 = S"Test";
System::String *str2 = new String("Test");
System::Console::WriteLine( (str1 == str2).ToString() ); // will be false,
                                                                            // because we check the adresses, not the entire objects
System::Console::WriteLine( (str1->Equals(str2)).ToString() ); // will be true


So what's going on there? Can anybody give me an explanation?
Do pinning pointers really pin objects or is there a bug?


Thanks in advance.
GeneralRe: Pinnig Pointers Pin
palbano1-Jul-04 17:43
palbano1-Jul-04 17:43 
GeneralRe: Pinnig Pointers Pin
EnemyMaker1-Jul-04 19:14
EnemyMaker1-Jul-04 19:14 
GeneralSubclassing a .NET winform control Pin
Matt Newman30-Jun-04 10:53
Matt Newman30-Jun-04 10:53 
GeneralRe: Subclassing a .NET winform control Pin
Csharp™2-Jul-04 16:51
Csharp™2-Jul-04 16:51 
GeneralRe: Subclassing a .NET winform control Pin
Matt Newman2-Jul-04 17:49
Matt Newman2-Jul-04 17:49 
Generalstd::queue and Managed class Pin
Vadim Tabakman28-Jun-04 19:00
Vadim Tabakman28-Jun-04 19:00 
GeneralRe: std::queue and Managed class Pin
palbano29-Jun-04 4:48
palbano29-Jun-04 4:48 
GeneralRe: std::queue and Managed class Pin
Vadim Tabakman29-Jun-04 13:17
Vadim Tabakman29-Jun-04 13:17 
GeneralRe: std::queue and Managed class Pin
adrian cooper5-Aug-04 1:49
adrian cooper5-Aug-04 1:49 
GeneralC++ help!! Pin
tyaramis24-Jun-04 23:09
tyaramis24-Jun-04 23:09 
GeneralRe: C++ help!! Pin
Kevin McFarlane26-Jun-04 4:06
Kevin McFarlane26-Jun-04 4:06 
GeneralThis tutorial looks better Pin
Kevin McFarlane26-Jun-04 4:15
Kevin McFarlane26-Jun-04 4:15 
GeneralPorting : MFC on Linux Pin
a_mlt124-Jun-04 22:14
a_mlt124-Jun-04 22:14 
GeneralRe: Porting : MFC on Linux Pin
Anthony_Yio28-Jun-04 18:03
Anthony_Yio28-Jun-04 18:03 
GeneralRe: Porting : MFC on Linux Pin
Anonymous16-Jul-04 7:35
Anonymous16-Jul-04 7:35 
GeneralPorting : MFC on Linux Pin
a_mlt124-Jun-04 22:12
a_mlt124-Jun-04 22:12 
GeneralRe: Porting : MFC on Linux Pin
Kevin McFarlane26-Jun-04 3:57
Kevin McFarlane26-Jun-04 3:57 

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.