gcnew und delete?
-
hab mal n kleines grundlagenproblem
mich irritierts ziemlich, dass ich zwar mit gcnew objekte erzeuge, aber die nicht mehr lösche
im moment erzeuge ich zum beispiel eine instanz einer netzwerkklasse sobald eine verbindung aufgebaut werden soll
wird die verbindung getrennt dann rufe ich einfach eine close methode auf und setze die referenz auf "nullptr" ( ihhhhh )
aber ich fühl mich garnich wohl dabeigibts eine möglichkeit objekte explizit zu zerstören?
btw. wann wird denn z.b. überhaupt der destruktor aufgerufen, falls z.b. der destruktor die verbindung trennen würde?
-
gcnew Objekte werden vom GC aufgeräumt.
Nebst dem ~T ist noch ein !T hinzugekommen.http://www.c-plusplus.net/forum/viewtopic-var-t-is-171647-and-highlight-is-boxing.html
Schau mal hier, der letzte Beitrag enthält ien kleines Beispiel. Geh das mal mit dem Debugger durch.
(Das !T gibt es AFAIK nur in C++/CLI )
-
danke
ich hab mir auch n bissi theorie durchgelesen
aber heisst das, dass sich der destruktor nicht mehr darum kümmern darf funktionale teile abzuschalten ( z.b. netzwerkverbindung schließen und socket freigeben ) weil ich ja nicht mehr festlegen kann wann er aufgerufen wird
-
Klar kannst Du das - mit delete
-
achsoo d.h. der gc is also nur n zusätzliches feature... aber wenn ich möchte kann ich weiterhin so arbeiten wie bisher richtig?
Obj^ pObj = gcnew Obj; delete pObj;
-
Jup, wenn Du die passenden Desturktoren einrichtest.
Das kleine Beispiel einfach mal mit Debugger angeschaut ?
-
ne ich mein garnich in erster linie eigene klassen... da schreib ich eh lieber native
aber wie reagieren denn die .net klassen auf delete?
-
Gute Frage...
http://msdn2.microsoft.com/en-us/library/ms379617(VS.80).aspx
This feels a lot more natural to a C++ programmer. I can free my resources in my destructor like I've always done. The compiler will emit the necessary IL to implement the IDisposable::Dispose method correctly, including suppressing the garbage collector's call to any Finalize method on the object. In fact, it is not legal in C++/CLI to explicitly implement the Dispose method. Inheriting from IDisposable will result in a compiler error. Of course, once the type is compiled, all CLI languages that consume it will see the Dispose pattern implemented in whatever way is most natural for each language. In C# you can call the Dispose method directly, or use a using statement just as if the type were defined in C#; but what about C++? How do you normally call the destructor of a heap based object? By using the delete operator, of course! Applying the delete operator to a handle will call the object's Dispose method. Recall that the object's memory is managed by the garbage collector. We are not concerned about freeing that memory, but only about freeing the resources the object contains.
Derived^ d = gcnew Derived(); d->SomeMethod() delete d;
So if the expression passed to the delete operator is a handle, the object's Dispose method is called. If there are no more roots connected to the reference type, the garbage collector is free to collect the object's memory at some point. If the expression is a native C++ object, the object's destructor is called before the memory is returned to the heap.
Certainly we're getting closer to the natural C++ syntax for object lifetime management, but it is still error-prone to have to remember to call the delete operator. C++/CLI allows you to employ stack semantics with reference types. What this means is that you can introduce a reference type using the syntax reserved for allocating objects on the stack. The compiler will take care of providing you the semantics that you would expect from C++, and under the covers meet the requirements of the CLR by actually allocating the object on the managed heap.
Soweit ich das bis dahin verstanden habe:
Das delete kann man sich wie ein delete auf ein Objekt mit Referenzcounting vorstellen.Der Bereich ist interesannt, da werd ich jetzt noch ne weile lesend mit verbringen. Les da auch mal bitte - mal schauen was Du daraus interpretierst
Bisher ging ich nach dem Experimentieren und einfachen Tests mit Formen davon aus, das ein delete reicht um den GC zu umgehen. Im Detail weiß ich es aber nicht - also hab ich da für mich erst mal undefiniertes verhalten.
Der GC stört mich aus den gleichen Gründen die Du nennst - und ich war froh das mit dem ~T und !T zu wissen
Auch interesannt sein dürfte:
http://msdn2.microsoft.com/en-us/library/ms379600(VS.80).aspx