Thread: My library is failing and I cannot figure out why...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    My library is failing and I cannot figure out why...

    NOTE: Feel free to laugh at me at any time, just don't flame me please

    Ok, its been a while since my last (first) thread here. And back then I was really naive. I probably still am, but I can't figure this out. I have searched here and on google, have looked in a couple of publications for help, but I am still stuck.

    I am writing this program that will take database exports and backups and generate reports from it. This library is the first part I decided to work on because, well, it just was. Remember, I in no way claim to know what the hell I am doing...

    First off, the problem I am having is that my library is failing when it tries to read a part of an object from memory (I think). The program just exits, no seg-fault, no abnormal termination... I do see a memory leak when I use memprof on it, but I am not sure what to do to avoid it. Sooooo.... without further ado, here is my class declaration: (this is the entire header file...)

    Code:
    // libpstate.h
    struct S_prog_error {
    	char *curblock;
    	char *calblock;
    	char *currargs;
    	char *currfunc;
    	char *errortxt;
    	char *errorarg;
    	char *errortyp;
    	unsigned int errcount;
    };
    
    struct S_prog_state {
    	char *library;
    	char *module;
    	char *function;
    	char *caller;
    	char *argument;
    };
    
    class C_prog_state {
    	public:
    		S_prog_state *psp[];
    		S_prog_error *pep[];
    		C_prog_state ( int num_of_states, C_prog_state *old );
    		~C_prog_state () {};
    		C_prog_state *ptt;
    		int states;
    		int write_state ( int state_index, char *state_str[5] );
    		int write_error ( int state_index, char *error_str[7] );
    		int read_states ( int state_index, char *ps [ 5 ] );
    		int read_errors ( int state_index, char *pe [ 7 ]);
    		int print_error ( int state_index );
    		int print_state ( int state_index );
    		int passer ( C_prog_state *ptr );
    };
    I don't know whether or not the problem lies there, but I can't post my library source just yet.... I have to put comments in so its readable... Let me know which definitions to post also so I don't have to post the whole thing (but I will if I have to)

    Thanks everyone

    (look for commented code in an hour or three)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What compiler/IDE are you using? Do you know how to use its debugger?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > char *curblock;
    If these are just strings, then use std::string for them, not char* arrays you have to allocate yourself.

    > S_prog_state *psp[];
    1. It's a waste of time using classes if you're just going to make everything public.
    2. This declaration makes no sense - either use S_prog_state **psp; or S_prog_state *psp[10];

    > int read_states ( int state_index, char *ps [ 5 ] );
    What exactly are you trying to do here?
    Post some code which shows both how you call this function, and the function itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Quote Originally Posted by Daved
    What compiler/IDE are you using? Do you know how to use its debugger?

    No IDE, using gcc 4.0.0. And no I have had no luck figuring out how to use the debugger...

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Quote Originally Posted by Salem
    > char *curblock;
    If these are just strings, then use std::string for them, not char* arrays you have to allocate yourself.

    > S_prog_state *psp[];
    1. It's a waste of time using classes if you're just going to make everything public.
    2. This declaration makes no sense - either use S_prog_state **psp; or S_prog_state *psp[10];

    > int read_states ( int state_index, char *ps [ 5 ] );
    What exactly are you trying to do here?
    Post some code which shows both how you call this function, and the function itself.
    I was originally going to use std::strings... Looks like I should have stuck with it...

    I realize everything is public, but that won't be the case in the end program. Right now I was more worried with getting the code to work before I decide what should be public and what shouldn't. Essentially, only the member functions and the pointer to the current object will be public.
    The declaration of S_prog_state *psp[] can be changed, there is no reason it should be one way instead of the other IMO... But it needs to point to an array of undefined size at compile time, at least the way the code is now....

    Bear with me on the code being posted, I have been swamped today, but I should be able to hit it in a few. Thanks for looking and check back in a little bit to see what it is I am trying to do. Sorry it has take so long, I should just learn to comment my code as I write it eh?

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    OK here it is finally....Terribly sorry for the wait

    And may the almighty moderators please please please forgive me for not being prepared when I should have been, for being generally inane, and for posting so many times just to get all the information regarding my question out in the open... Please don't smack me too hard.

    Lets assume for the moment that I want to use char* instead of std::strings for an undisclosed reason. Just show me where I am doing something other than what is needed.

    I don't know whether or not I should code the whole thing or add it as an attachment.... I guess when in doubt, I shall do the latter. (it is all of 236 lines)

    On a side note, I have made a minor change to the header I already posted that makes things a little simpler in a small part of libpstate.cpp.

    in S_prog_error, the variable errcount has been removed and now appears in the body of the C_prog_state class....

    in libpstate.h:
    in S_prog_error {}:
    Code:
    	unsigned int errcount;
    has been removed.
    and in C_prog_state {}:
    Code:
    	unsigned int errcount;
    has been added.

    Don't worry, I am not going to keep modifying the thing until I have a strategy to fix my issues. It still compiles and behaves the same as before.

    Speaking of compiling..... my Makefile is here too:
    Code:
    libpstatetest: libpstatetest.cpp
    	g++ -O3 -Wall -c -o libpstate.o libpstate.cpp
    	g++ -O3 -Wall -o libpstatetest libpstate.o libpstatetest.cpp
    I get no errors at compile time, which would be the case if I have a bad memory allocation problem... (I think???) Anyhow here is the output I get when I run the test program:

    [root@MYBOX libpstate]# ./libpstatetest
    Program State Information: (State Level: 0)
    Current library:
    Current module: [root@MYBOX libpstate]#


    I know I am doing many things wrongly and many more wierdly. But this is how I learn (by doing things wrong) and, amazing as it is, it is how I keep myself interested in learning (by doing things wierdly).

    I am also posting the test program I wrote to test this library. It is small so I will just code it.

    Code:
    // libpstatetest.cpp
    #include "libpstate.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main () {
    	C_prog_state cps = C_prog_state ( 1, (C_prog_state*)NULL );
    	cps.print_state ( 0 );	// This is the function call in which the prog
    				// abruptly stops with no appearent reason.
    	cps.print_error ( 0 );
    	return 0;
    }
    There is also an alternate library source file that fails at a different place, but while dealing with the same peice of memory. I will not modify the Makefile included in the text of this post, but the Makefile I will upload will be able to "make libpstatetest.alt" as well as "libpstatetest" for both implementations..

    I will attach all source files as well as the Makefile. I will append a .txt to the end of the Makefile so it will let me upload it. I wish I could upload a gzipped tarball containing everything, but oh well... (Keep in mind, libpstate.alt.cpp does not have comments in it)

    Thanks again, and once more, I apologize profusely for my delays and poor skills. At least I am working on improving them, utilyzing my entire budget of negative money

    Look forward to hearing from you guys soon.

    Have a good weekend as well... My internet connection is flaky at home and I may not be able to log in to check the status of this. But I will be back Monday.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Gdb

    Quote Originally Posted by DerelictDream
    No IDE, using gcc 4.0.0. And no I have had no luck figuring out how to use the debugger...

    Ok I took the time to figure out the GNU debugger...

    compile with the added option -g to add support for gdb...

    I tried gdb with just the -g added to the makefile, but I would get seg-faults every time I tried to set a breakpoint. I switched the optimization to none (-O0 instead of -O3) and tried it again. Here is what I got from the debugger:


    [root@MYBOX libpstate]# gdb libpstatetest
    GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
    Copyright 2003 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...
    (gdb) add-symbol-file libpstate.o
    add symbol table from file "libpstate.o" at
    (y or n) y
    Reading symbols from libpstate.o...done.
    (gdb) break C_prog_state:rint_state
    Breakpoint 1 at 0x8048cd7: file libpstate.cpp, line 138.
    (gdb) run
    Starting program: /test/tbsreports/dev/libpstate/libpstatetest

    Breakpoint 1, C_prog_state:rint_state(int) (this=0xbfffe168, state_index=0)
    at libpstate.cpp:138
    138 << C_prog_state:sp [ state_index ] ->argument << "\n";(gdb) n
    Program State Information: (State Level: 0)
    Current library:
    139 return 0;
    (gdb) n
    140 };
    (gdb) n
    main () at libpstatetest.cpp:12
    12 cps.print_error ( 0 );
    (gdb) n
    13 return 0;
    (gdb) n
    14 }
    (gdb) n
    0x42015704 in __libc_start_main () from /lib/tls/libc.so.6
    (gdb) n
    Single stepping until exit from function __libc_start_main,
    which has no line number information.
    Current module:
    Program exited normally.


    This is interesting... It looks like it is going on to the next function after doing only part of the output from the first. But the only other output it does is STILL from the first function. Maybe I should just give up and start over and not use anything complicated in my code.... Oh well

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Hrmmm...I just tried something different...

    I changed the initialization of C_prog_state:ep and C_prog_state:sp in libpstate.h from:

    Code:
    		S_prog_state *psp[];
    		S_prog_error *pep[];
    to:
    Code:
    		S_prog_state **psp;
    		S_prog_error **pep;
    This required that I change the definitions in libpstate.cpp from:
    Code:
            for ( int i = 0; i < C_prog_state::states; i++ ) {
                    C_prog_state::psp [ i ] =
                            new S_prog_state;
                    C_prog_state::pep [ i ] =
                            new S_prog_error;
    ...
    to:
    Code:
    	*C_prog_state::psp = new S_prog_state [ C_prog_state::states ];
    	*C_prog_state::pep = new S_prog_error [ C_prog_state::states ];
    Now I get seg fault when I run it and when I debug it, it dies at the same place as before, but the output is somewhat different...


    [root@mmphtnws312 libpstate]# gdb libpstatetest
    GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
    Copyright 2003 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for details.
    This GDB was configured as "i386-redhat-linux-gnu"...
    (gdb) add-symbol-file libpstate.o
    add symbol table from file "libpstate.o" at
    (y or n) y
    Reading symbols from libpstate.o...done.
    (gdb) break C_prog_state:rint_state
    Breakpoint 1 at 0x8048e9b: file libpstate.cpp, line 139.
    (gdb) run
    Starting program: /test/tbsreports/dev/libpstate/libpstatetest

    Breakpoint 1, C_prog_state:rint_state(int) (this=0xbffff130, state_index=0)
    at libpstate.cpp:139
    139 << C_prog_state:sp [ state_index ] ->argument << "\n";(gdb) n
    Program State Information: (State Level: 0)
    Current library:
    140 return 0;
    (gdb) n
    141 };
    (gdb) n
    main () at libpstatetest.cpp:12
    12 cps.print_error ( 0 );
    (gdb) n
    13 return 0;
    (gdb) n
    14 }
    (gdb) n
    0x42015704 in __libc_start_main () from /lib/tls/libc.so.6
    (gdb) n
    Single stepping until exit from function __libc_start_main,
    which has no line number information.
    Current module:
    Program exited with code 0250.



    But since no one seems to be able to help me, I guess I will go away now.

Popular pages Recent additions subscribe to a feed