• C

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.
edrusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
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
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
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
 
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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.
Jaime OlivaresSoftware ArchitectCommented:
Are many processes trying to access the same code portion where the fopen is?
I suspect that because of your 'static' declaration of arquivo
edrusAuthor Commented:
Hi Jaime,

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

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

Thanx
moduloCommented:
PAQed with points refunded (250)

modulo
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.