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

Few compile errors when building using an Atmega128 #502

Open
rickyelqasem opened this issue Dec 24, 2024 · 3 comments
Open

Few compile errors when building using an Atmega128 #502

rickyelqasem opened this issue Dec 24, 2024 · 3 comments

Comments

@rickyelqasem
Copy link

Atmega128 is an AVR / Megacore using the Arduino framework so presumed sdfat would compile without errors but I got a few:

.pio\libdeps\Upload_ISP\PCM\TMRpcm.cpp:1442:61: error: use of deleted function 'SdFile::SdFile(const SdFile&)'
unsigned long pos = searchMainTags(xFile, datStr);
^
In file included from .pio\libdeps\Upload_ISP\PCM\TMRpcm.cpp:9:0:
.pio\libdeps\Upload_ISP\SdFat\src/SdFat.h:468:7: note: 'SdFile::SdFile(const SdFile&)' is implicitly deleted because the default definition would be ill-formed:
class SdFile : public PrintFile {
^~~~~~
.pio\libdeps\Upload_ISP\SdFat\src/SdFat.h:468:7: error: use of deleted function 'PrintFile::PrintFile(const PrintFile&)'
In file included from .pio\libdeps\Upload_ISP\SdFat\src/ExFatLib/ExFatFile.h:883:0,
from .pio\libdeps\Upload_ISP\SdFat\src/ExFatLib/ExFatVolume.h:27,
from .pio\libdeps\Upload_ISP\SdFat\src/ExFatLib/ExFatLib.h:28,
from .pio\libdeps\Upload_ISP\SdFat\src/SdFat.h:31,
from .pio\libdeps\Upload_ISP\PCM\TMRpcm.cpp:9:
.pio\libdeps\Upload_ISP\SdFat\src/ExFatLib/../common/ArduinoFiles.h:43:7: note: 'PrintFile::PrintFile(const PrintFile&)' is implicitly deleted because the
default definition would be ill-formed:
class PrintFile : public print_t, public BaseFile {
^~~~~~~~~
.pio\libdeps\Upload_ISP\SdFat\src/ExFatLib/../common/ArduinoFiles.h:43:7: error: 'FsBaseFile::FsBaseFile(const FsBaseFile&)' is private within this context
In file included from .pio\libdeps\Upload_ISP\SdFat\src/FsLib/FsLib.h:31:0,
from .pio\libdeps\Upload_ISP\SdFat\src/SdFat.h:33,
from .pio\libdeps\Upload_ISP\PCM\TMRpcm.cpp:9:
.pio\libdeps\Upload_ISP\SdFat\src/FsLib/FsFile.h:75:3: note: declared private here

@greiman
Copy link
Owner

greiman commented Dec 24, 2024

The problem is in this call searchMainTags(xFile, datStr) a copy constructor is used for xFile.

This can cause file corruption since the state of the file is lost when the constructed object is destroyed on return from searchMainTags(). See this.

@rickyelqasem
Copy link
Author

So I changed #define FILE_COPY_CONSTRUCTOR_PRIVATE 1 to #define FILE_COPY_CONSTRUCTOR_PRIVATE 2 and the problem went away but can you explain why that happened?

@greiman
Copy link
Owner

greiman commented Dec 25, 2024

but can you explain why that happened?

I disable the copy constructor since SdFat is designed to have only one instance of the File object for an open file. The state of the file is maintained in the file object. If you call a function with a copy of the file object, any changes of the file state will be lost.

See this.

You should design functions with File object arguments as call by reference or call by pointer, not call by value.

File myFile;

void callByReference(File& file) {
    file.write(123);
}
void callByPointer(File* file) {
  file->write(123);
}
void callByValue(File file) {
  file.write(123);
}
void test() {
  callByReference(myFile); // OK
  callByPointer(&myFile); // OK
  callByValue(myFile);  // Error
}

You are lucky that it works in your app but if you have problems don't post an issue.

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

No branches or pull requests

2 participants