. NET short talk about interoperability (4: Basic knowledge-based Dispose unmanaged memory)

Source: Internet
Author: User

Series of articles on interoperability :. NET (1: Introduction ). NET (2: Faster first ). NET (3: The DllImport feature of basic knowledge ). NET (4: Basic knowledge-based Dispose unmanaged memory ). NET (5: Basic knowledge-based Dynamic platform call ). NET (6: Improving platform calling performance based on basic knowledge ). NET (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, just to elaborate on the above issues; it is not that easy to release the unmanaged memory in the managed memory; different code libraries, the calling methods for allocating memory are different, and the algorithms are different. The allocation and recovery methods for C are malloc, free, new and delete for C ++, and CoTaskMemAlloc and CoTaskMemFree for COM; 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 );

. 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:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1912495A5-0.png "/>

This is a non-hosted code. Due to the time relationship, I did not write specific operations; just explain the principles;

Figure 2:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/191249C35-1.png "/>

This is the unmanaged code call relationship defined on the managed. NET platform;

Figure 3:

650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1912492938-2.png "/>

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 used. Wang qingpei has All Rights Reserved.

This article is from the pattern driven the world blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/599284

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.