Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

gcroot i fiksirani (pinned) pokazivači

[es] :: .NET :: gcroot i fiksirani (pinned) pokazivači

[ Pregleda: 3896 | Odgovora: 13 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.121.eunet.yu.



+9 Profil

icon gcroot i fiksirani (pinned) pokazivači16.07.2006. u 03:32 - pre 187 meseci
Ako imamo sledeći kod...

Code:

#include <iostream>
#include <vector>
#include <vcclr.h>
#include <gcroot.h>
#using <mscorlib.dll>

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace std;

__gc class Ivana // prost omotač
{
    public:
        Ivana(){
            id = 19;
        }
        int id;
};

int main(int argc, char *argv[])
{
    Ivana *obja = new Ivana;

    vector<gcroot<Ivana*> > v;
    v.push_back(obja);

        // Mene zanima da li je u ovom trenutku objekat, na koji pokazuje pokazivač obja,
        // fiksiran nakon dodavanja vektoru (barem mislim da jeste...)?
        // Odnosno, da li funkcija GCHandle::Alloc fiksira objekat nakon pravljenja handle-a s njom...?

    system("pause");
}


Hvala unapred.
 
Odgovor na temu

NrmMyth
Ivan Maček
Split

Član broj: 63456
Poruke: 849
*.cmu.carnet.hr.

Sajt: www.dump.hr


Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 12:59 - pre 187 meseci
Jeli to ime cure ili jos gore, mame ze klasu?! LOL

Evo ti jedan dio iz knjige "Programming with Managed Extensions for Microsoft Visual C++ .NET":
Citat:
C++ Standard Library

You can use the C++ standard library, but be aware that you cannot use it with managed types. So you can neither put managed objects directly into Standard Template Library (STL) containers, nor write stream operators for managed types. If you want to integrate managed objects and native objects, you have to write wrapper classes, either managed wrappers around your native types so that they can be put into a managed container or a native wrapper so that a managed type can be put into an STL container. STL code can generate C++ exceptions, so if your code uses STL, you have to enable exception handling with the /EH switch.
Managed Pointers in Unmanaged Types

I mentioned earlier that an unmanaged type cannot contain a managed pointer because the garbage collector will not be able to track the managed pointer. However, there are cases when it is useful for an unmanaged type to treat a managed type as a “data member,” and to do this, the unmanaged type must explicitly handle the lifetime of the managed object.

The .NET Framework has a class named GCHandle that represents a reference on a managed object. A GCHandle is a value type that has sequential layout and does not have managed members, but because of the bug I mentioned earlier (the compiler ignores the sequential layout attribute), GCHandle cannot be a member of a __nogc type. A GCHandle can be converted to an IntPtr—in other words, as a platform specified integer—which can be converted to an int, and hence, a GCHandle can be stored indirectly in a __nogc type.

GCHandle does not have an accessible constructor; instead, you initialize an instance by calling the Alloc method. This method is overloaded, and both versions take an object reference from which the GCHandle will be initialized. The value in the GCHandle instance acts like a pinning pointer: while the value exists, the object will be pinned, so it will not be moved in memory or collected. However, unlike a pinning pointer, you must take explicit steps to unpin the object by calling GCHandle::Free, as shown in this example:

// filewriter.cpp
__nogc class FileWriter
{
public:
FileWriter(String* name)
{
StreamWriter* sw = File::AppendText(name);
GCHandle h = GCHandle::Alloc(sw);
IntPtr ptr = GCHandle::op_Explicit(h);
writer = ptr.ToInt32();
}
~FileWriter()
{
StreamWriter* sw = GetStream();
sw->Close();

GCHandle h = GCHandle::op_Explicit(IntPtr(writer));
h.Free();
}
void Write(String* s)
{
StreamWriter* sw = GetStream();
if (sw) sw->Write(s);
}
private:
int writer;
StreamWriter* GetStream()
{
GCHandle h = GCHandle::op_Explicit(IntPtr(writer));
return static_cast<StreamWriter*>(h.Target);
}
// Prevent copying, see later
FileWriter(const FileWriter&){}
operator=(const FileWriter&){}
};

This unmanaged class wraps a StreamWriter object; the object is created in the constructor, and it is released in the destructor. Because this class is unmanaged, it can be created on the stack. Therefore, it allows you to hold onto the managed resource only as long as the stack frame survives.

The constructor takes a managed string as a parameter. This arrangement is fine because the managed string reference lives only as long as the stack frame of the method call. The constructor uses this reference to create a StreamWriter object. The constructor then creates a GCHandle based on this object—which effectively pins the object. The handle is first converted to an IntPtr and then converted to an int so that the handle can be stored in the unmanaged type.

The instance of the unmanaged FileWriter class must release its managed resources when it is destroyed. The first action the object performs is to close the file. The object then unpins the StreamWriter object by converting the integer to an IntPtr, converting the IntPtr to a GCHandle, and then calling Free. The private method GetStream returns the StreamWriter object by accessing the GCHandle::Target property, which is the object that has been pinned.

You might decide to hold more than one managed type in an unmanaged class. However, the code to access these objects and their GCHandle structures would get rather messy. To help out, the C++ team has provided the unmanaged template gcroot<> in gcroot.h, as shown here:

template <class T> struct gcroot
{
gcroot();
gcroot(T t);
gcroot(const gcroot& r);
~gcroot();
gcroot& operator=(T t);
gcroot& operator=(const gcroot &r);
operator T () const;
T operator->() const;
};

The template parameter, T, is a managed pointer type. The class will always create a GCHandle for the managed object passed to the constructor; the copy constructor and assignment operator also create new handles based on the object that the gcroot<> instance wraps. This arrangement is so that if an instance of a class that uses a gcroot<> is copied, there will be two separate GCHandle structures for the same object. This duplication is needed because if both instances of gcroot<> had the same handle, destroying one of the instances would invalidate the other instance of gcroot<>. (In the preceding FileWriter example, I have ignored this issue by making the copy constructor and assignment operator private.)

The gcroot<> template has conversion operators that return the object wrapped by the handle and an operator-> so that you can treat the gcroot<> as a smart pointer class. The FileWriter class using gcroot<> looks like this:

// filewriter2.cpp
__nogc class FileWriter
{
gcroot<StreamWriter*> writer;
FileWriter(const FileWriter&){}
operator=(const FileWriter&){}
public:
FileWriter(String* name)
{
writer = File::AppendText(name);
}
~FileWriter()
{
writer->Close();
}
void Write(String* s)
{
writer->Write(s);
}
};

I think you’ll agree that this code is far clearer than the previous version.
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
*.hsd1.ma.comcast.net.



+6 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 13:19 - pre 187 meseci
Koliko znam, ne. Zašo te uopšte to interesuje?

Inače, ako ćeš da radiš sa starim MC++m i STL-om, napisao sam jedan članak o tome pre par godina: http://www.codeproject.com/managedcpp/gcstl.asp
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.157.eunet.yu.



+9 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 13:20 - pre 187 meseci
Citat:
NrmMyth:
Evo ti jedan dio iz knjige "Programming with Managed Extensions for Microsoft Visual C++ .NET":
...The value in the GCHandle instance acts like a pinning pointer...


Hvala, ostalo je poznato, a ovo mi je bilo neophodno...


Citat:
NrmMyth: Jeli to ime cure ili jos gore, mame ze klasu?! LOL


Devojke... Ali njih u stvarnom životu ne mogu da imam više od jedne na heapu...
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.157.eunet.yu.



+9 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 13:24 - pre 187 meseci
@Dragi Tata
Video sam taj header... Zato sam donekle i pitao, kako bih uspeo da razmem... Koliko sam shvatio, ti si napisao odgovarajuće function objekte za poređenje koji implementiraju ICompareble ako sam dobro razumeo, zaista fina stvar, nego ja htedoh da je bolje razumem...
 
Odgovor na temu

NrmMyth
Ivan Maček
Split

Član broj: 63456
Poruke: 849
*.cmu.carnet.hr.

Sajt: www.dump.hr


Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 14:37 - pre 187 meseci
U zadnje vrijem sam dosao do zakljucka da sve vise ljudi prelazi na MC++ (C++/CLI)... to mi je drago.

Citat:
Dragi Tata: Koliko znam, ne.

Nepotrebno je pinati objekt jer takve akcije samo stvaraju fragmentaciju u GC memoriji. GChandle ce pokazivati na pravo mjesto u GC memoriji i nakon defragmentacije. Bitno je jedino da instanca ne bude collectana.
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
*.hsd1.ma.comcast.net.



+6 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 21:28 - pre 187 meseci
Citat:
Goran Arandjelovic:  Koliko sam shvatio, ti si napisao odgovarajuće function objekte za poređenje koji implementiraju ICompareble ako sam dobro razumeo, zaista fina stvar, nego ja htedoh da je bolje razumem... :)


Jeste, kao što možeš da vidiš, napisao sam tri stvari: functore za poređenje, verziju za ostream_iterator i wrapper za kolekcije koji radi sa STL algoritmima.

Međutim, sve to je napisano za stari MC++. Nemam pojma šta od toga radi sa C++/CLI i šta uopšte ima smisla.
 
Odgovor na temu

NrmMyth
Ivan Maček
Split

Član broj: 63456
Poruke: 849
*.cmu.carnet.hr.

Sajt: www.dump.hr


Profil

icon Re: gcroot i fiksirani (pinned) pokazivači16.07.2006. u 22:21 - pre 187 meseci
...zbog STL.NET.
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.102.eunet.yu.



+9 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači17.07.2006. u 01:15 - pre 187 meseci
@Dragi Tata
Kada kažeš "stari MC++", misliš na .NET 1.1 ili se varam...?
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
*.hsd1.ma.comcast.net.



+6 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači17.07.2006. u 01:44 - pre 187 meseci
Ne znam koliko si u toku, ali razvoj MC++a je obustavljen i razvija se novi jezik C++/CLI. http://en.wikipedia.org/wiki/C%2B%2B/CLI
 
Odgovor na temu

NrmMyth
Ivan Maček
Split

Član broj: 63456
Poruke: 849
*.cmu.carnet.hr.

Sajt: www.dump.hr


Profil

icon Re: gcroot i fiksirani (pinned) pokazivači17.07.2006. u 10:50 - pre 187 meseci
I vrlo ces brzo shvatiti zasto.
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.120.eunet.yu.



+9 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači17.07.2006. u 16:04 - pre 187 meseci
Javljaju mi se određene nejasne stvari. C++/CLI jeste nov jezik sa (koliko vidim) nekim razjašnjenim i novim mogućnostima i čistijom sintaksom u odnosu na stari MC++, ali predpostavljam da je korišćenje framework-a (1.x, 2.0, i kasnije 3.0) nezavisno od toga da li pišem u starom MC++-u ili u C++/CLI?
 
Odgovor na temu

NrmMyth
Ivan Maček
Split

Član broj: 63456
Poruke: 849
*.cmu.carnet.hr.

Sajt: www.dump.hr


Profil

icon Re: gcroot i fiksirani (pinned) pokazivači17.07.2006. u 17:02 - pre 187 meseci
Ne.
C++/CLI nije kompatibilan sa verzijama .NET-a prije 2.0.
 
Odgovor na temu

Dragi Tata
Malo ispod Kanade

Član broj: 1958
Poruke: 3906
*.lionbridge.com.



+6 Profil

icon Re: gcroot i fiksirani (pinned) pokazivači18.07.2006. u 14:21 - pre 187 meseci
Sa druge strane, možeš da koristiš stari MC++ sa .NET 2.0 (opcija /clroldsyntax ako se ne varam), ali ti stvarno savetujem da se ne upuštaš u to. Ja sam svojevremeno dosta koristio MC++ za "mixed assemblies" i to je sad propala investicija.

Inače, trenutno ide interesantna anketa na CodeProject-u: http://www.codeproject.com/script/survey/detail.asp?survey=586
 
Odgovor na temu

[es] :: .NET :: gcroot i fiksirani (pinned) pokazivači

[ Pregleda: 3896 | Odgovora: 13 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.