Android NDK crash when reading/writing files

微笑、不失礼 提交于 2019-12-10 12:17:50

问题


I have ported a very large application to Android. It receives binary data over TCP/IP, and writes it to files which it uses now and then in the application. The directory to which it saves the files is by default set to ./file_cache. It goes wrong here:

fileHandle = fopen(filename,"wb");

filename in this case is "file_i" where i starts from 0 and increments for every file.

The program crashes on writing, and it doesn't have to crash on the first file, sometimes it gets as far as the 10th file. When it crashes the fileHandle ends up as NULL.

The filename is initially in wchar, but is converted before (I print it out in the logger to confirm that it is correct).

What I have tried:

  • I have added all permissions that seem to have anything to do with file read/write external storage.

  • I have tried different locations /sdcard/files and /data/data/<package>/files

Questions:

  1. What is the current directory, i.e. where do the files really end up on the default setting?

  2. What can be causing fopen to return null, which in turn is the cause of the crash?

Very thankful for any ideas.


回答1:


I found the problem, though it was a general program error. The filename "file_i" was due some complicated code being assigned size 1, which naturally was too small, causing the segmentation fault. So it had nothing to do with fopen. Actually, all segmentation faults I've had have been caused by a fault similar to this so it can be worth really scrutinizing your code whenever you are messing with char/wchar.

Moreover, I use the /data/data/.. folder which works fine, haven't actually tried the sdcard one but can't see why it would not work.



来源:https://stackoverflow.com/questions/13016057/android-ndk-crash-when-reading-writing-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!