On Sun, 2007-12-16 at 06:34 -0700, Thomas Hruska wrote:
> Michael Sullivan wrote:
> > OK. I need help tracking down a segmentation fault. The problem is
> > that even with the backtrace information from gdb, I'm still not
> exactly
> > sure where it is occurring. Here's the backtrace info:
> >
> > Program received signal SIGSEGV, Segmentation fault.
> > [Switching to Thread 0x4000 (LWP 31519)]
> > 0xb7f2d69c in do_lookup_x () from /lib/ld-linux.so.2
> > (gdb) backtrace
> > #0 0xb7f2d69c in do_lookup_x () from /lib/ld-linux.so.2
> > #1 0xb7f2da3e in _dl_lookup_symbol_x () from /lib/ld-linux.so.2
> > #2 0xb7f317c4 in _dl_fixup () from /lib/ld-linux.so.2
> > #3 0xb7f36db0 in _dl_runtime_resolve () from /lib/ld-linux.so.2
> > #4 0x08049bd3 in battle::battle ()
> > #5 0x0804a09f in main ()
> >
> > It looks to me like something in my battle constructor is causing
> it,
> > but the battle constructor creates four Allies, which makes calls to
> > Character and the whole thing's a mess. I don't even know what code
> to
> > post here. I've got the full source code of everything the project
> > currently uses at
> http://www.espersunited.com/~michael/needhelp.txt .
> > Please help, if you can. If there's any other information I can
> provide
> > that would be useful, please let me know...
>
> I took a look at the source. IMO, you are doing WAY too much in the
> constructors. A _LOT_ of stuff can fail. Library calls that can fail
> should be reserved for a call to your own initialization function
> AFTER
> construction. I like to use Init(). Constructors should be fairly
> lightweight (just initialize variables).
>
> battle mybattle = battle();
>
> That line is unnecessary and is probably the cause of your problems.
> Just do:
>
> battle mybattle;
>
> The code before was creating a battle structure (mybattle). Then it
> created a second battle structure (temporary object). Then it called
> the default assignment operator, which did a surface copy of the
> temporary object to mybattle. Then the temporary object was deleted
> (destructor called) which made all the mybattle pointers _invalid_
> because they are just ordinary pointers _copied_ from the temporary
> object. Attempting to use the invalid pointers is probably what
> caused
> the application to crash.
>
> I highly recommend reading Safe C++ Design Principles. Particularly
> the
> sections involving smart pointers. Pretty sure I included source code
> to a fully functional smart pointer. The C++ compiler is smart enough
> to know how to do a deep copy BUT only if every single variable in
> the
> class is either a C++ class, template, or a base data type other than
> a
> pointer and all those know how to do deep copies as well.
>
I was poking around the Files section yesterday and noticed that the
Safe C++ book is still an .exe.
> I'd like to take this time to point out that you've got your graphics
> engine (including initialization and shutdown) tied directly into a
> class that apparently will manage battles. These are, IMO, two
> separate
> logic units that should be treated as such.
>
> --
> Thomas Hruska
> CubicleSoft President
> Ph: 517-803-4197
>
> *NEW* MyTaskFocus 1.1
> Get on task. Stay on task.
>
> http://www.CubicleSoft.com/MyTaskFocus/
>
>
>
>
>