fclose

GlibC Double free or corruption (fclose)

二次信任 提交于 2019-12-02 21:46:10
问题 I got an error on my C program on runtime. I found some stuff about "double free or corruption" error but nothing relevant. Here is my code : void compute_crc32(const char* filename, unsigned long * destination) { FILE* tmp_chunk = fopen(filename, "rb"); printf("\n\t\t\tCalculating CRC..."); fflush(stdout); Crc32_ComputeFile(tmp_chunk, destination); printf("\t[0x%08lX]", *destination); fflush(stdout); fclose(tmp_chunk); printf("\t[ OK ]"); fflush(stdout); } It seems the fclose(tmp_chunk);

GlibC Double free or corruption (fclose)

二次信任 提交于 2019-12-02 09:29:59
I got an error on my C program on runtime. I found some stuff about "double free or corruption" error but nothing relevant. Here is my code : void compute_crc32(const char* filename, unsigned long * destination) { FILE* tmp_chunk = fopen(filename, "rb"); printf("\n\t\t\tCalculating CRC..."); fflush(stdout); Crc32_ComputeFile(tmp_chunk, destination); printf("\t[0x%08lX]", *destination); fflush(stdout); fclose(tmp_chunk); printf("\t[ OK ]"); fflush(stdout); } It seems the fclose(tmp_chunk); raises this glibc error : *** glibc detected *** ./crc32: double free or corruption (out): 0x09ed86f0 ***

Is it possible to rescue file descriptor from FILE*?

雨燕双飞 提交于 2019-12-02 05:45:17
I have to use a certain cross-platform library which passes FILE* objects around. I get a file descriptor from another source (inherited), I want to keep same fd across fork 'd processes. I currently use fdopen to convert a file descriptor to a FILE* object. My problem is that fclose used to clean up FILE* objects closes connected file descriptor. I would very much like to keep this file descriptor after it has been used. is there a way rescue file descriptor from FILE* ? Is there a way to detach it? Or a way to substitute a file descriptor in FILE* with a dummy? P.S. this needs to be cross

Why would fclose hang / deadlock? (Windows)

拥有回忆 提交于 2019-12-01 16:08:21
问题 I have a directory change monitor process that reads updates from files within a set of directories. I have another process that performs small writes to a lot of files to those directories (test program). Figure about 100 directories with 10 files in each, and about 500 files being modified per second. After running for a while, the directory monitor process hangs on a call to fclose() in a method that is basically tailing the file. In this method, I fopen() the file, check that the handle

Exists a way to free memory in atexit or similar without using global variables?

≯℡__Kan透↙ 提交于 2019-12-01 05:54:01
I am developing a project in C, and I need to free the allocated memory and also close all the open files before it exits. I decided to implement a clean function that will do all this stuff and call it with atexit because there are a lot of possible exit scenarios. The problem is that atexit doesn't allow me to set functions with parameters, so I can't send to clean the pointers that need to be freed in the end of the process. So I need to declare as global variables every pointer that may need to be freed, and every file that may remaining open in the program? (I already did that but doesn't

Create a file if one doesn't exist - C

廉价感情. 提交于 2019-11-29 20:09:18
I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr = fopen("scores.dat", "rb+"); if(fptr == NULL) //if file does not exist, create it { freopen("scores.dat", "wb", fptr); } You typically have to do this in a single syscall, or else you will get a race condition. This will open for reading and writing, creating the file if necessary. FILE *fp = fopen("scores.dat", "ab+"); If you want to read it and then write

Fclose a file that is already fclose

Deadly 提交于 2019-11-29 10:51:40
In my programm I may close a file that is already close. What happen when I do a fclose on a file already close ? And if you can't do so, how to know if a file is closed or open ? Calling fclose twice with the same stream is undefined behaviour - most likely crash. There is no way to check if FILE* has been closed already, so the safe solution is to set pointer to NULL as soon as it is closed: fclose(fh); fh = NULL; Sources: "The value of a pointer to a FILE object is indeterminate after the associated file is closed" (C draft standard). "After the call to fclose(), any use of stream causes

Create a file if one doesn't exist - C

和自甴很熟 提交于 2019-11-28 16:26:38
问题 I want my program to open a file if it exists, or else create the file. I'm trying the following code but I'm getting a debug assertion at freopen.c. Would I be better off using fclose and then fopen immediately afterward? FILE *fptr; fptr = fopen("scores.dat", "rb+"); if(fptr == NULL) //if file does not exist, create it { freopen("scores.dat", "wb", fptr); } 回答1: You typically have to do this in a single syscall, or else you will get a race condition. This will open for reading and writing,

How exactly does fopen(), fclose() work?

℡╲_俬逩灬. 提交于 2019-11-28 05:33:25
I was just wondering about the functions fopen, fclose, socket and closesocket. When calling fopen or opening a socket, what exactly is happening (especially memory wise)? Can opening files/sockets without closing them cause memory leaks? And third, how are sockets created and what do they look like memory wise? I'm also interrested in the role of the operating system (Windows) in reading the sockets and sending the data. Disclaimer: I'm mostly unqualified to talk about this. It'd be great if someone more knowledgeable posted too. Files The details of how things like fopen() are implemented

C - Working with fopen, fclose, fputc etc

…衆ロ難τιáo~ 提交于 2019-11-28 02:17:29
I've got this code finally working with a single argument on my command line, i.e. one file for it to work with, although I designed the code with the concept of it working with an unlimited number of files. What it does is take some X number of text files containing words separated by spaces, and replaces spaces with \n thus creating a list of words. Though, it successfully completes the first argument, it just ignores the 2nd. Another minor problem seems that it also prints out some garbage letter at the end, a Y with two dots above it; I assume some EOF symbol, yet I can't seem to stop that