debug gives different result than run

M

marcus

I have this class A that contains a method A_method that opens a file
and does fgets.

I also have a template class B that contains a method B_method that
takes a class A object as template type. B_method then uses this A
object to call A_method.

here is a snippet (pseudo)

In class A.cpp:
===============
B <A> myVar;

// method that uses method of template class
A::someMethod(){
myVar.B_method(this);
 
L

Larry Brasfield

marcus said:
I have this class A that contains a method A_method that opens a file
and does fgets.

I also have a template class B that contains a method B_method that
takes a class A object as template type. B_method then uses this A
object to call A_method.

here is a snippet (pseudo)

In class A.cpp:
===============
B <A> myVar;

// method that uses method of template class
A::someMethod(){
myVar.B_method(this);
.
.
}

// method to be called from template class
A::A_method(char *fileName){
char iLine[200];
int meshCount = 0;

FILE *stream = fopen(fileName, "r");
//geometryData = NULL;
if(stream != NULL){
// loop through parsing every line in the file
while (fgets(iLine, 200, stream) != NULL){
.
.
}
}
}

In class B.h:
=============
B<TAG>::B_method(TAG *A_object)
{
.
.
A_object->A_method(fileName)
}

The 1st method that is invoked above is A::someMethod
The problem is that when running the application the fgets makes the
application crash (the _cnt component has the value -1). When
debugging the application though everything works fine.


Your shown code has become a bit too pseudo.
There is no _cnt component and from what is
shown, there is no visible reason for the fgets()
call to fail with a fault.

It is common, when one has induced undefined
behavior, for that behavior to change when the
build settings change.

You may want to provide some real but reduced
code that duplicates your problem. Alternatively,
learn to modify your non-debug build so that you
can use the debugger and debug it.
 
C

Chris Theis

marcus said:
I have this class A that contains a method A_method that opens a file
and does fgets.

I also have a template class B that contains a method B_method that
takes a class A object as template type. B_method then uses this A
object to call A_method.

here is a snippet (pseudo)

In class A.cpp:
===============
B <A> myVar;

// method that uses method of template class
A::someMethod(){
myVar.B_method(this);
.
.
}

// method to be called from template class
A::A_method(char *fileName){
char iLine[200];
int meshCount = 0;

FILE *stream = fopen(fileName, "r");
//geometryData = NULL;
if(stream != NULL){
// loop through parsing every line in the file
while (fgets(iLine, 200, stream) != NULL){
.
.
}
}
}

In class B.h:
=============
B<TAG>::B_method(TAG *A_object)
{
.
.
A_object->A_method(fileName)
}

The 1st method that is invoked above is A::someMethod
The problem is that when running the application the fgets makes the
application crash (the _cnt component has the value -1). When
debugging the application though everything works fine.

Code that exhibits such behavior is very likely to cause some undefined
behavior in previous sections before your program breaks down. Most probably
the problem is not fgets() but might be found before. Use your debugger to
break before fgets is executed and check the variables & pointers that are
around.

HTH
Chris
 
M

marcus

Thanks for answering.

Larry Brasfield said:
Your shown code has become a bit too pseudo.
Yes maybe I am sorry for this, I will shortly try to post some code
that is more of the actual code I am using.
There is no _cnt component
the _cnt component is an element of the FILE datatype. I think it is
supposed to have the value of the number of bytes read (this showing
-1 when I run is not a good sign :-( ).
You may want to provide some real but reduced
code that duplicates your problem.
I will try to do that shortly
 
M

marcus

Problem solved!

You were right, I had a pointer that was used before the call to
A_method that did not seem to be pointing correctly.

Thanx to both of you for your attention.



Chris Theis said:
marcus said:
I have this class A that contains a method A_method that opens a file
and does fgets.

I also have a template class B that contains a method B_method that
takes a class A object as template type. B_method then uses this A
object to call A_method.

here is a snippet (pseudo)

In class A.cpp:
===============
B <A> myVar;

// method that uses method of template class
A::someMethod(){
myVar.B_method(this);
.
.
}

// method to be called from template class
A::A_method(char *fileName){
char iLine[200];
int meshCount = 0;

FILE *stream = fopen(fileName, "r");
//geometryData = NULL;
if(stream != NULL){
// loop through parsing every line in the file
while (fgets(iLine, 200, stream) != NULL){
.
.
}
}
}

In class B.h:
=============
B<TAG>::B_method(TAG *A_object)
{
.
.
A_object->A_method(fileName)
}

The 1st method that is invoked above is A::someMethod
The problem is that when running the application the fgets makes the
application crash (the _cnt component has the value -1). When
debugging the application though everything works fine.

Code that exhibits such behavior is very likely to cause some undefined
behavior in previous sections before your program breaks down. Most probably
the problem is not fgets() but might be found before. Use your debugger to
break before fgets is executed and check the variables & pointers that are
around.

HTH
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top