A bold guess for the Object Pascal compiler to assign a stack of memory details (below)

zhaozj2021-02-11  197

In order to here, you may say that it is a half-day, it is guess, maybe, the OP compiler will not call the TOBJECT.NEWINSTANCE method at all!

Asked, do experiment!

Or the TBASE class above is an example, and the TOBJECT.NEWINSTANCE method is overloaded, as follows:

TBASE = Class (TOBJECT)

x: integer;

Y: Double;

Class Function Newinstance: TOBJECT; OVERRIDE

Excedure FreeInstance; Override;

Constructor crete;

END;

{achieve}

Constructor tbase.create;

Begin

Self.x: = 2;

Self.y: = 3.14;

END;

Procedure TBASE.FREEINSTANCE;

Begin

inherited;

ShowMessage (Format ('Call% S.FreeInstance !!!', [Self.className]));

END;

Class function TBASE.NEWINSTANCE: TOBJECT;

Begin

ShowMessage (Format ('call% s.newinstance "; [self.classname]);

Result: = inherited newinstance;

END;

Then a simple statement of the document:

VAR

B: TBASE;

Begin

B: = TBASE.CREATE; ß here breaks the point!

B.free;

END;

Call the newinstance method immediately by tracking the code.

[Description: Be sure to overload it to track it, at the breakpoint, observe the CPU, can be found in the dislapsed code, call a _classcreate first, then call newinstance]

The same method can be analyzed that B.free will finally call to FreeInstance; to release the object.

I think the big problem has been said, Object Pascal In order to achieve allocation stack memory, when you call the constructor:

B: = TBASE.CREATE;

Before your code in the construction method, the code calls the NewInstance method. When you analyze, you call the FreeInstance function after your code in the destructor.

So, now I will see this situation: derived

TBASE = Class (TOBJECT)

x: integer;

Y: Double;

Class Function Newinstance: TOBJECT; OVERRIDE

Excedure FreeInstance; Override;

Constructor crete;

END;

TSUB = Class (TBASE)

M: integer;

N: Double;

Constructor crete;

END;

{achieve}

Constructor tbase.create;

Begin

Self.x: = 2;

Self.y: = 3.14;

END;

Procedure TBASE.FREEINSTANCE;

Begin

inherited;

ShowMessage (Format ('Call% S.FreeInstance !!!', [Self.className]));

END;

Class Function TBASE.NEWINSTANCE: TOBJECT; Begin

ShowMessage (Format ('call% s.newinstance "; [self.classname]);

Result: = inherited newinstance;

END;

{TSUB}

Constructor tsub.create;

Begin

Inherited Create; ß Note here!

Self.m: = 4;

Self.n: = 12.32;

END;

We already know,

VAR

S: TSUB;

s: = tsub.create;

When I entered Tsub.create, I got the memory it wanted immediately [here is 32 bytes], then when:

Inherited Create, when in TBASE.CREATE, there is a memory allocation? We can prove three points: Here, TBASE.CREATE is only completed the initialization code given by the programmer, and does not perform the actions of memory allocation.

The first point, RETURNVALUE: = inherited create; the resulting return address and the return address obtained by calling TSUB.CREATE.

Second, if you allocate new memory again in TBASE.CREATE, then

Self.x: = 2;

Self.y: = 3.14;

Just for new memory operations, while the original S object is inherited from TBase, Y will not change, or 0, but we found that X, y has changed, so it can also prove TBase.create no Allocate new memory, just set up the x, y in the original memory.

Third point, track. This is the easiest way to see inherited create; do not call NewInstance, experiment prove, not called.

However, if INHERITE.CREATE in tsub.create; the situation is different, it is discovered in three ways, it is assigned a new stack of memory, which not only does not meet the destination of programmer initialization data. However, it causes memory leaks, and such bugs are difficult to find.

That is, the compiler is found that if it is called the constructor through the class, it will be constructed as a new class object, and allocate a stack memory. deal with. I think, for Anders Hejlsberg [Delphi Designer], I want to achieve such a function in the compiler is not a difficult thing [In fact, we can analyze the Central Plains by viewing assembly code. If you are interested, please pay attention to this Test D1, D1 instructions and under jump instructions below.

PS: I have just told this book "Delphi's Atom World", I really want to get it. If you have its E-Book version, I hope you can send it to me: CODER@vip.sina.com

转载请注明原文地址:https://www.9cbs.com/read-5745.html

New Post(0)