Whether it frees memory the right way?

my demo code

f_update() will build Msg very fast, but I don't know whether it frees memory the right way.

thanks.

well, I didn't make it clear. I mean the place where it logs the "dropping msg ...". f_getlast() will not print the log.

f_getlast() only returns a copy of the id of the current msg, it does not drop (or create, or copy) any Msg. Each old message is automatically dropped before a new one is stored in the static variable. The last message is never dropped, as static variables is not dropped before the program terminates.

If you want every message to be dropped, you can set the static variable to None before terminating. But since both the updaing and the reading thread assumes that there will always be a message, you then need to make sure those are terminated before setting the None (or change their implementation to terminate when they see None).

1 Like

thanks.

I have another question. 'Each old message is automatically dropped before a new one is stored in the static variable.', this is an important thing for static variable, but where can I find this feature in documentation or any reference book?

That part is not specific to static variable, it would happen for a local variable as well (but the last Msg would be dropped when the local variable goes out of scope, so the difference is just that a static variable never goes out of scope).

I thought dropping the old value on assignment would be mentioned in Running Code on Cleanup with the Drop Trait - The Rust Programming Language , but that just talks about things going out of scope. And I don't find a clear description in Variables and Mutability - The Rust Programming Language either. Maybe this could be improved in the book?

Edit: What is Ownership? - The Rust Programming Language would also be a good place to describe this, but I can't find a clear description there either.

It can be found in The Rust Reference

Here: Operator expressions - The Rust Reference

Assignment expressions

Syntax
AssignmentExpression :
   Expression = Expression

An assignment expression moves a value into a specified place.

An assignment expression consists of a mutable place expression, the assigned place operand , followed by an equals sign (=) and a value expression, the assigned value operand.

Unlike other place operands, the assigned place operand must be a place expression. Attempting to use a value expression is a compiler error rather than promoting it to a temporary.

Evaluating assignment expressions begins by evaluating its operands. The assigned value operand is evaluated first, followed by the assigned place operand.

Note: This is different than other expressions in that the right operand is evaluated before the left one.

It then has the effect of first dropping the value at the assigned place, unless the place is an uninitialized local variable or an uninitialized field of a local variable. Next it either copies or moves the assigned value to the assigned place.

An assignment expression always produces the unit value.

Example:

let mut x = 0;
let y = 0;
x = y;

Note the part about

It then has the effect of first dropping the value at the assigned place, unless the place is an uninitialized local variable or an uninitialized field of a local variable.

3 Likes

thanks

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.