Delphi Multi-line Cheng

Source: Internet
Author: User

A few days ago, debugging a bug, the process first not to say, the final debugging to MM, that is, debug DCU, and then into the getmem.inc in the Get/freemem function, after the appearance of Av.

And then find ... Depressed n days, and later found that the MM switch to qmm after all normal, and then cut back to the original Mm,bug appeared ...

By experience, this type of problem is generally caused by a thread not having a lock, but it is not found.

OK, also not very drop, suddenly think of ismultithread variable, think of MM get/free/realloc all need this thing to protect, and VCL, only one place to operate this variable: classes.tthread.create-> System.beginthread

Then I remembered that I had not used TThread, created the thread with Api.createthread, and it did not set true to Ismultithread, the problem was found and the bug was lifted.

Consequently, this article records this issue.

Bug Formation:

1: The program does not use TThread or beginthread to create the thread, so that it does not operate on Ismultithread

2: Use API.Windows.CreateThread to create thread without ismultithread true

3: In the created thread, Get/free/reallocmem and interacting with other threads (such as the main thread) that block of memory

Bug Show:

1: The bug will cause MM data structure error, the problem is big go.

When AV appears, watch looks at the data is wrong, and randomly give you different data

Call stack is also silly, navigate to Infinity Code ...

In short, everything is silly, not to believe.

Bug avoidance:

1: Caution with API.Windows.CreateThread, if you want to operate on it, please remember ismultithread: = True;

If you do not remember, please refer to the code: System.beginthread

or create a thread directly using System.beginthread.

2: Using QMM,QMM is automatic maintenance ismultithread, the number of threads N (n>1), Ismultithread=true, n=1, ismultithread = false;

This, the other mm, I looked at, did not do the processing. (I'm advertising)

Personal advice is: the development environment must be more than two sets of MM, in order to spare, encountered some inexplicable problems, switch, to confirm whether the MM problem caused.


Note:

Line Cheng: in end.
Initialization
Ismultithread: = True;

Mm=memory Manager, D2005 version starts to integrate FASTMM.

  However, from the D7 (D5 not notice, should also be) Getmem.inc, until FastMM4.991 all rely on the ISMULTITHREAD flag for thread protection, not self-maintenance of the flag.

Therefore, when this bug condition is formed, it will appear: I know that because the multithreading does not protect the data caused by the confusion caused by the bug, but is not find where to trigger, how to solve. This is the most pit daddy.  :)

Delphi Multi-line Cheng

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.