• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

fopen() is generating error

Hi all,

I have a app made in C Ansi that works with sockets (WinXP SP2 and WEdit C Compiler).
At sometime the app dies, and why this error is occurring?

In debugger mode, i found that a call to fopen() was killing my program.

---------------------------------------------
Ex. Code:
...
#if (defined(LOG) || defined(DEBUG))
  static FILE * arquivo = NULL;
#endif
...
// begin of infinite loop
...
#ifdef LOG
  arquivo = fopen("logfile.txt","a+t");
  if (arquivo)
  {
     fprintf(arquivo, ...); // some code here
     fclose(arquivo);
     arquivo = NULL;
   }
#endif

...

#ifdef DEBUG
  arquivo = fopen("temp.txt","wt");
  if (arquivo)
  {
     fprintf(arquivo,...); // some code here
     fclose(arquivo);
     arquivo = NULL;
   }
#endif
...
// if last entity, then break loop
// end of infinite loop
---------------------------------------------

Anyone have idea?

Thanx a lot.
Edro.
0
edrus
Asked:
edrus
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
This code seems just fine.

Offhand, I suspect that something else in your program is writing over the memory used by the stream process.


Kent
0
 
sunnycoderCommented:
Hi edrus,

I too agree with Kent ... The code snippet looks fine.

Application may be crashing due to memory corruption caused by some other segment of code as Kent indicated or may be a hiesenbug.

Try finding a pattern for the crash. Does it always crash on the same fopen? How many times do you need to open and close the file to make it crash? Is this number constant?

Is there some kind of synchronization invovled?

you can try stepping into fopen until it crashes and note down the memory location which it was trying to access when it crashed. For the next run, set watch on these memory locations.

Make sure you are not writing to uninitialzed of free()'ed pointers. Also make sure that you are not violating any array bounds.

These kind of bugs can take substantial amount of time and effort to fix.

cheers
sunnycoder
0
 
kledbetterCommented:
What error code is getting returned from the fopen() call?  Print out the value of errno and then you'll be able to track down why the call is failing.  Something else, as others have said, may be overwriting memory -- but you didn't post real code, so I can't tell if you're trashing the stack or not.

-Keith
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
edrusAuthor Commented:
Keith,
---------------------------------------------------------------------------------------------------
Is Strange to say to you what error is returning, because when call the fopen(), the application dies. I don't know why :(

I tried to put some code to debug like the following, and printed only the first printf():

...
printf("Before call fopen...\n");
arquivo = fopen("logfile.txt","wt");
printf("After call fopen...\n");
...

????
---------------------------------------------------------------------------------------------------
Sunnycoder (Answering your questions),

Is crasinhg in the same fopen(). The prog works with sockets, so i open 4 apps (in different folders) with ports 6000,7000,8000,9000 respectively. Only one port is crashing, and the others works fine (???... with the same executable)... i.e., Ports 6000, 7000 and 8000 terminated successfuly and 9000 was crashed. Is this a hardware problem? I don't belelieve that may be!

Don't have any sinchronization in code!

I'll try to step memory location os this pointer.... and i'll tell you any notice soon after!
---------------------------------------------------------------------------------------------------

Thanx for all!
Edro.
0
 
Jaime OlivaresCommented:
Are many processes trying to access the same code portion where the fopen is?
I suspect that because of your 'static' declaration of arquivo
0
 
edrusAuthor Commented:
Hi Jaime,

No... Just only one process accessing that code portion!

I tried to remove 'static' declaration, but not works....

Thanx
0
 
moduloCommented:
PAQed with points refunded (250)

modulo
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now