Interoperability Series Article :
- . Net short talk about interoperability (1: Introduction At the beginning)
- . Net short talk about interoperability (2: Faster first)
- . Net short talk about interoperability (3: Basic knowledge of dllimport features)
- . Net short talk about interoperability (4: Basic knowledge-based dispose unmanaged memory)
- . Net short talk about interoperability (5: Basic knowledge-based dynamic platform call)
- . Net short talk about interoperability (6: Improving platform calling performance based on basic knowledge)
- . Net short talk about interoperability (7: Introduction to data sending)
We continue to learn about. Net interoperability. In the previous article, we learned the key features of dllimport in the basic knowledge. We continue to learn about the memory Release Technology in the basic knowledge;
Between managed and non-managed memory, direct calls are not allowed to perform operations on each other, which I believe is a little bit. anyone with the net technology base can understand it. A friend in the previous article raised some questions, so here we should just take a rough analysis. The problem is probably like this:[Wang qingpei has all rights reserved. For more information, please sign it.]
1. Can objects on the. NET hosting platform be directly exchanged with non-hosted objects?
2. Are there any differences between managed memory and non-managed memory? If there are differences, is there a way to perform equivalent conversion or equivalent replication;
Let's take a closer look at the above two questions. As I have learned this knowledge, I have not understood it very well. I can only make some small guesses, let's help with the analysis;
The first question: can objects on the. NET hosting platform be directly exchanged with non-hosted objects? In fact, the first problem is hidden in the second problem. First, we need to determine the concept of "Interchange" and "Conversion" to unify everyone's pace, we must define "Interchange" and "Conversion;
"Swap": I assume there are two pieces of memory space, each of which stores different objects. For example, a char * type pointer is stored in an unmanaged memory block, the value of the string type is stored in another managed memory block. Because char * is a pointer type, our managed string is. NET platform type, small changes may lead to different memory layout; as mentioned in the data structure, variables are divided into atomic and structural types, atomic variables all have the concept of the word value, what is the literal value is the data value we use for communication, for example, the boolean type is true or false; the two memories store different things, after processing a series of compilers, the advanced language determines what the data stored in the memory looks like. That is to say, the memory allocation principle is based on the object type; in the hosted and unmanaged memory space, all the reference address types on different platforms are also different, of course, a successful "swapping" means that direct data access can be performed between hosting and non-hosting. In short, swapping is a two-way conversion between two objects;
"Conversion": I personally think the concept of conversion comes from the syntax interpretation of advanced languages. The so-called conversion is actually the conversion from A to B, convert one type to another type of Verb description. For example, if one object is a and one object is B, I want to convert A to B, that is, to convert an object from one State to another. In short, the conversion is unidirectional and can only be one to another;
In view of the above analysis results, due to the tight time, let's start with the second problem, because this article is not mainly about solving the problem; after the above analysis, we confirm that the structure of the managed memory and the non-managed memory is different. This is different, not all object types are different, in. NET platform has some platform equivalent types such as int and char, which can be directly exchanged. If it is a non-equivalent type, to make a successful conversion, you must use the knowledge of hosting objects on interoperability. This article may not end up, so let's take a rough look; let's start with today's theme. In fact, some concepts are really not very easy to talk about. If you want to talk about hosting and non-hosted memory are different, some people will ask where they are different. There is really no reason for vigilance;
About the release of unmanaged memory
It seems that today's topic is about hosting and non-hosting memory. It just gives a detailed description of the above problem. It is not that easy to release the unmanaged memory in the managed memory; differentCodeLibrary, calling different methods for allocating memory,AlgorithmThe distribution and recovery of c are malloc, free, C ++ is new and delete, and COM is cotaskmemalloc and cotaskmemfree; there are thousands of memory operations on a platform with such a large operating system, maybe the technician wrote a set of DLL for memory allocation and recovery, so we cannot. net once and for all, so there are these uncertainties. (no matter what we call the underlying layer, even if we have crossed the kernel to the hardware abstraction layer or even to the driver part, 1 is 1, 2 or 2, the journey is still going. We should never be at the bottom layer of the mystery, as long as we hold a heart of exploration, everything is OK );:[Wang qingpei has all rights reserved. For more information, please sign it.]
. The default memory allocation and recovery on the net platform are based on COM (Component Object Model). Because COM is a set of public principles in the unmanaged age, Microsoft can only do this; if the unmanaged memory is allocated with COM cotaskmemalloc.. NET will automatically release the block of memory. If the unhosted memory is C, C ++, or other methods. net does not know how you allocate it, so we need to take a compromise to solve this problem. The release of the unmanaged memory is only known to the unmanaged database. Therefore, you can define a method to release the unmanaged Resources in the unmanaged database, and then use it.. NET platform calls this unmanaged method to release the memory. Here is a small example to illustrate the problem;
Figure 1:
This is a non-hosted code. Due to the time relationship, I did not write specific operations; just explain the principles;
Figure 2:
This is the unmanaged code call relationship defined on the managed. NET platform;
Figure 3:
In this way, no matter how the unmanaged memory is used to allocate the memory, we can release it in the hosting; The first principle is that we must understand the allocation method of the unmanaged memory; if it is unclear, the default release method of COM is ;:[Wang qingpei has all rights reserved. For more information, please sign it.]