Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RAII support for File class #67

Closed
wants to merge 1 commit into from
Closed

Add RAII support for File class #67

wants to merge 1 commit into from

Conversation

willeccles
Copy link

When a File object goes out of scope, its resources are not freed. This patch:

  • Adds a destructor to free this memory and close the file handle when the object is destroyed.
  • Deletes the copy constructor and copy assignment operator, as this class is not copyable (at least, not without more significant future work).
  • Adds move semantics.

Fixes #66.

When a File object goes out of scope, its resources are not freed. This
patch:

- Adds a destructor to free this memory and close the file handle when
  the object is destroyed.
- Deletes the copy constructor and copy assignment operator, as this
  class is not copyable (at least, not without more significant future
  work).
- Adds move semantics.

---

Fixes #66.
@willeccles
Copy link
Author

CI fails because the example sketches are broken and attempt to copy the file structure. I can either fix the examples or implement copying for this class.

@willeccles
Copy link
Author

I suggest fixing the examples. Copying a file handle like this is generally discouraged and can cause some really awful problems. The offending example could easily be fixed by taking File& instead of File (or even File* if you prefer, but I don't see any advantage to using a pointer here).

@fpistm
Copy link
Member

fpistm commented Sep 28, 2023

Well example is based on the official example.
So API changed and it is not desired. Ex:
https://github.com/arduino-libraries/SD/blob/master/examples/listfiles/listfiles.ino

@willeccles
Copy link
Author

Ah, that is a bit annoying. I could implement copying for files here, but that's definitely not recommended... Perhaps it would be best to close this PR. It's not the first time I've run into Arduino's C++ code being sub-par :/

To implement copying, it's trivial to duplicate the file name, but I don't think there's any reasonable way to manage copies of the file handle itself. Re-opening isn't an option, as this will break if file locking is enabled, and if it's not, could lead to potential races. Copying the file handle directly would work, but then if one copy closes the file, the other is in an unknown state. As this also applies to the existing implementation, it's probably not worth changing.

@willeccles willeccles closed this Sep 28, 2023
@fpistm
Copy link
Member

fpistm commented Sep 28, 2023

Anyway, I understand your point and it is valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File class is not RAII friendly
2 participants