The program runs when the user deletes the working directory and crashes

Source: Internet
Author: User

Recently encountered such a problem, Android run program, in the program initialization will create its own working directory, storage some cache, log and configuration files.

If the user deletes its working directory during the run of the program, the program will find that the directory is inaccessible and then recreate the directory the next time it is used, but the creation will fail, resulting in a subsequent crash.

The reason for the creation failure is errno 16:ebusy (Device or resource busy), meaning that the current directory is already occupied and cannot be overridden by creating a directory with the same name.

I am depressed, the original directory is not already access (access () or file.exists ()), why this problem?

Problem Resolution : Before the user deletes a directory, a file pointer to the file in the program is not closed, that is, the program is still referencing the file in the directory, so this directory is not deleted. After a user has performed a delete operation, the operating system is recognized even if the directory is not available in the File Manager, and the directory is actually present. Therefore, the next time the program needs to access the directory, it is found that the directory does not have access, so it does not think this directory exists (in fact, it is inaccessible), so you want to create a new directory with the same name, and the operating system found that this is to overwrite the existing directory operation, there is a directory is being referenced, not overwritten, Therefore, program considers that the creation of the directory failed, resulting in subsequent failures.

In short, in a word, the problem is that you deleted the file and didn't close it before .

workaround : Before the file or directory is deleted, ensure that all file pointers are closed, that is, all references to the files are closed. But how do I know when the file will be deleted by (preface users)?

One way to do this is to open the file and close the file each time you manipulate the file, which avoids long-term references, greatly reducing the probability of the above problems. However, for some file operations frequently, it is inefficient to open files every time, such as log operations, often to write log files. In this case, long-term holding of the file pointer is efficient, but will encounter previous problems. My practice is that every time you access the file pointer, you have to determine whether the current path is still intact, if the path does not exist, then close all the file pointers in the current directory, so as to ensure that no longer hold the relevant file references, so that the current directory can be deleted normally, then re-create the directory, and then recreate the file, and open the file again to save the file pointer.

The program runs when the user deletes the working directory and crashes

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.