Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 03:02 AM
marcus
Guest
 
Posts: n/a
Default debug gives different result than run

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);
  #2  
Old July 23rd, 2005, 03:02 AM
Larry Brasfield
Guest
 
Posts: n/a
Default Re: debug gives different result than run

"marcus" <marcus.silfver@koping.net> wrote in message
news:1a55433d.0503141102.145db97@posting.google.co m...[color=blue]
>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.[/color]


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.

--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.


  #3  
Old July 23rd, 2005, 03:03 AM
Chris Theis
Guest
 
Posts: n/a
Default Re: debug gives different result than run


"marcus" <marcus.silfver@koping.net> wrote in message
news:1a55433d.0503141102.145db97@posting.google.co m...[color=blue]
> 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.[/color]

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


  #4  
Old July 23rd, 2005, 03:06 AM
marcus
Guest
 
Posts: n/a
Default Re: debug gives different result than run

Thanks for answering.

"Larry Brasfield" <donotspam_larry_brasfield@hotmail.com> wrote in message
[color=blue]
> Your shown code has become a bit too pseudo.[/color]
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.
[color=blue]
> There is no _cnt component[/color]
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 :-( ).
[color=blue]
> You may want to provide some real but reduced
> code that duplicates your problem.[/color]
I will try to do that shortly
  #5  
Old July 23rd, 2005, 03:07 AM
marcus
Guest
 
Posts: n/a
Default Re: debug gives different result than run

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" <Chris.theis@no-spam.cern.ch> wrote in message news:<d16bo8$hh7$1@sunnews.cern.ch>...[color=blue]
> "marcus" <marcus.silfver@koping.net> wrote in message
> news:1a55433d.0503141102.145db97@posting.google.co m...[color=green]
> > 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.[/color]
>
> 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[/color]
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles