I dunno why "why" has to come up in a tech question with a tech answer, but here goes:
Because different languages and programming paradigms require you to approach problems differently. This is a tautology. What you have asked is how do to the same thing in two different languages without really explaining what that same thing is. You have claimed that you simply want to replicate object destruction, but it is clear that object destruction for you is more than just cleaning up resources, as you also seem to be handling reference counting in addition to resource destruction. Reference counting is usually not necessary in managed languages as once references to an object go out of scope, the resources that are used by the object are free to be released automatically. Of course there are exceptions to the rules, but that explains why people want more information concerning your approach and your needs.
I code in C++. I like C++. It can work on Windows, it can work on iOS, it can work on Linux, the only place it can't work is where Microsoft has decided thou shalt use C# and C# thou shalt use.
This is not even remotely true. The only time you need to use C# is when you want access to the .NET libraries, but this is hardly required for any project (although it is a valuable tool). And when working in .NET, you have access to multiple languages, not just C#. Perhaps much of your frustration is due to inexperience or a lack of education regarding the platform, and giving us more information concerning your problem will help us find the gaps in your knowledge to better arm you for the future?
I use very vanilla C++. I'm not a trickster with it. Most of my C++ can actually go to Java, even, with minimal changes. The only exception being destructors, which as I said above, I use heavily.
Here is the deal: Memory management in managed languages (Java, .NET, python, etc.) is a completely different paradigm from memory management in C++. Your issue seems to stem from a desire to try and equate the two. This is why we need more information: there is no easy answer to your problem because you are trying to do something that is fundamentally flawed.
I am trying to make my C++ delete my objects, while at the same time make it so that I can move the whole shebang over to C# if I should need to. I am working toward future compatibility. I am not using C# at this time.
In C++, you must manually delete heap allocated objects. In managed languages such as Java and C#, you do not need to manually delete heap allocated objects. Once all references to an object go out of scope, the memory allocated by that object is eligible to be "garbage collected" by the runtime. There is nothing the programmer needs to do to make this happen, other than ensure that they are no longer holding onto a reference to an object.
However, if your object is doing more than simply holding on to other managed objects (such as using an unmanaged resource) or requires additional logic at the time it is no longer needed by the application, then you must take additional steps. In C#, you have finalizers
, which allow you to let go of unmanaged resources at the time the garbage collector decides to completely destroy your object. However, finalizers are non-deterministic, you never know when they will be invoked and you can never manually invoke them, because it is up to the garbage collector and the runtime. To compensate for this, .NET also uses the Dispose pattern
. This allows for both deterministic deallocation of resources (by manually calling the dispose method or by implementing the using
construct), and for non-deterministic resource deallocation, as if you forget to call dispose, it will be called by the garbage collector when it decides it can safely delete your object.
I am trying to give myself a consistent workaround, that I'll call all the time, to delete objects, which won't actually delete them on a managed language, but WILL invoke a "Destroy()" function that I can do housekeeping that I'd normally do in a destructor. I want this set up so I can use it fairly consistently in C++ and C# (small exceptions would be fine).
First of all, as we have already mentioned, trying to achieve parity between a managed and unmanaged language when it comes to memory management is a Bad Idea™. What I want to know is, what is the housekeeping you are doing in a destructor of your C++ objects that you need to do in C# that does not involve cleaning up allocated resources. This is the general purpose of destructors in C++, and if you are doing something else, you are not creating trivial destructors, so we need to know what makes your destructors so nontrivial. Your given example includes reference counting, but once again, we need to know what and why you are reference counting because this is often a process that is made unnecessary by managed languages.
For my own sanity, I want it compatible with arrays and other stuff, so that I have margin to be sloppy.
Now, I know the Array delete is possible... I have a solution above, but was wondering if a better one exists. The "call Destroy()" function thing I want is probably *not* possible, but I'm asking on the chance that someone has ever accomplished that.
You keep saying you want to delete objects, but at the same time you don't want to delete them. Can you see the fundamental disconnect here? What do you want Destroy() to do? Why do you want the object to continue living after calling Destroy()? Why can't you simply let the array fall out of scope or call Clear() on your List<>? What housekeeping do you need to do?
On the philosophy of programming: I know "change everything you're doing and do it my way" is always an option. But I am looking for a way to not "change everything I'm doing."
C++ and C# are miles apart as far as approach and paradigm. So are C++ and Java. There is no way you can #define your way into compatible solutions. If you are unwilling to adapt to the paradigms presented by your chosen tools, then all I can say is that you are Doing It Wrong™.