printf() Stops My Application from Carshing?!

Aug 24, 2012 at 6:58am
THIS is ridiculous! Just today I've been reassuring myself that the compiler isn't evil and doesn't want to play tricks on me when this happens:

I opened a 2 weeks old project, built it (again)... At the very very end, the point where I save my data, the program crashed over and over again. Next, the program started to crash at the very beginning, over and over again! :(

So I added a printf() to see where the program is crashing, to my surprise, it no longer crashed at the beginning, but only at the end. Removed the printf(), the program crashed at the beginning, like a boss, over and over again!

I'm using Code::Blocks... Suggestions may keep me from going insane.
Last edited on Aug 24, 2012 at 6:58am
Aug 24, 2012 at 7:03am
If you supply some code we'd be able to get a better idea. There's got to be a reason for it.
Aug 24, 2012 at 7:06am
closed account (o3hC5Di1)
Also, have you tried running it through a debugger, comparing the results with / without printf?

All the best,
NwN
Aug 24, 2012 at 8:34am
Okay, I don't know how to pass arguments for main through the debugger but here is what I did...

two versions:

1
2
int argc = 6; char *  argv[6] = {"main.exe", "here\abc.ext", "here\xyz.ext", "3", "5", "21"};
int main()


and this

int main(int argc, char ** argv)

It seems that the application cannot seem to find the file "here\abc.ext"! I followed it till here:

1
2
3
if((fp=fopen(filename,"rb"))==NULL) {
        printf("%s is not found.\n",filename);
        exit(-1);


The funny thing is that it is printing this:
hereabc.ext is not found.


What's also weird is that in the debug/release builds in realtime mode, the application doesn't even display this sentence and just stops working...
Last edited on Aug 24, 2012 at 8:37am
Aug 24, 2012 at 8:41am
You need to escape your '\'s.

"here\abc.ext"
is:
"here(some weird ascii beep code or something random)bc.exe"

You need to escape the \s with another \.
Aug 24, 2012 at 9:54am
@firedraco

But it was working fine for the last two months, and it still does with other projects! :D

Could you please give me an example of that ascii beep code?
Aug 24, 2012 at 11:29am
haha..you knw, it kinda funny when this happens. Dude, don't blame the compiler, its does its job very efficiently. Check you input , in my opinion, it may be the root oft the problem.

Good luck, n yea, post your source code, none of us
can tell anything without the source code.
Last edited on Aug 24, 2012 at 11:29am
Aug 24, 2012 at 11:48am
¿Did you check that the file is actually there? it is relative to the working directory.
But it was working fine (...)
You were unlucky, do it properly.

Project->Set program's arguments
   Debug
Last edited on Aug 24, 2012 at 11:48am
Aug 25, 2012 at 7:36am
@ne555

It IS there! And the application was able to read the source file the first couple of times but wasn't able to output into the destination. In the next runs, the application couldn't even read the source file it seems. Sometimes I added a printf() somewhere and it was able to read the source file, but still couldn't output in the destination file...


Yes I'll try that when I get to work, I had the entire folder archived someplace else, when I copied it back to my work directory, it did this weird behavior, and these are two instances that didn't throw in error:


 
int * p = (double **) malloc(3*sizeof(double *));

and

1
2
3
4
5
6
7
8
9
10
11
12
13
static void function1(FILE * fp, char * c)
{
     // ...
     function2(FILE * , char * , int);

     function2(fp, filename, 10);
     // ...
}

static void function2(File * fp, char * c, int i)
{
     // ...
}


This gave no error in MSVC and MinGW on Code::Blocks the first 50 times, but when I intended to compile a DLL, it gave an error.

I'll keep you updated what happens, since I'm supposed to wrap up my work at this point not redo it :D

Thanks all!
Last edited on Aug 25, 2012 at 7:42am
Aug 25, 2012 at 12:14pm
By the way
1
2
3
4
if((fp=fopen(filename,"rb"))==NULL) {
        //printf("%s is not found.\n",filename);
        perror("fopen"); //will tell what went wrong
        exit(-1);


I don't understand what you meant with those snips.
Aug 25, 2012 at 2:32pm
@ne555

Ah you mean the code snippets. Yes, See I did almost 50 or so versions of the code (don't ask me why), and each time I include the header and the definitions file, which include function1() and function2(). I compiled the code more than 500 times (not kidding), and not once did the compiler raised an error.

One day I wanted to wrap my function, i.e. main() in a DLL, only then, my compiler raised an error.

Aug 27, 2012 at 1:25am
Okay, so here's some information:


Reason why application crashed at starting point in realtime mode: no clue (but it stopped)

Reason why application crashed at starting point in Debug mode: the data was in the Release and Debug folders. In debug mode, data should be stored in the project directory o_O

Reason why application crashes at the end: a segmentation fault when attempting to fprintf() a header to the file specified by a filename...

EDIT: Now I'm getting another SIGSEGV at the beginning! To hell with it I'm not gonna waste my time on this... If it I found out why I'm having this I'll tell you guys.

Thanks!
Last edited on Aug 27, 2012 at 2:22am
Topic archived. No new replies allowed.