#Trouble figuring out crash

157 messages ยท Page 1 of 1 (latest)

tardy sage
#

Thread 1 "r-type_client" received signal SIGSEGV, Segmentation fault. 0x0000000000484be0 in UIDGenerator::generate (this=0x50) at /home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/src/utils/UIDGenerator.cpp:18 18 unsigned int res = _uid;

class UIDGenerator {
    public:
        UIDGenerator();
        ~UIDGenerator();
        unsigned int generate();
    protected:
    private:
        unsigned int _uid = 0;
};

unsigned int UIDGenerator::generate() 
{             |
    //lign 18 v
    unsigned int res = _uid;

    _uid++;

    return res;
}
deep pumiceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

fossil sail
#

intresting that that segfaults ๐Ÿค”

#

how is your call for this? as in what code calls the generate

marble igloo
#

the call is on an invalid object

#

so all members are at invalid addresses

fossil sail
#

yup that was where I wanted to go :)

#

cause this code by itself is fine, so it has to be on the calling side pretty much

tardy sage
#

So it would be the UIDGenerator instance that doesn't exist

fossil sail
#

yup

#

my guess is you have a UIDGenerator* somewhere

tardy sage
#
    UIDGenerator _uidGen;
std::shared_ptr<GameObject> gameObject = std::make_shared<GameObject>(
            _game, _uidGen.generate(), name, position);
#

sadly not

#

it's just a regular instance

fossil sail
#

๐Ÿค” odd

marble igloo
#

that says absolutely nothing about where the instance is

fossil sail
#

yup

#

cause if it's right above the code that should be fine

tardy sage
#
class GameObjectManager {
private:
    UIDGenerator _uidGen;
};
fossil sail
#

oh

#

yeah

tardy sage
#

and GameObjectManager does the previously mentionned call

#

yeah what

fossil sail
#

๐Ÿค” and where does that other call happen

tardy sage
#
std::weak_ptr<GameObject> addGameObject(std::string name, Vector3 position) {
        unsigned int uid = _uidGen.generate();
        std::shared_ptr<GameObject> gameObject = std::make_shared<GameObject>(
            _game, uid, name, position);
        _toBeAdded.push_back(gameObject);
        return gameObject;
    }
fossil sail
#

nah I thought to quickly about something but doesn't have to be true

tardy sage
#

I changed the call a bit to see if it changed anything

fossil sail
#

and addGameObject is a member function?

tardy sage
#

but still same error

#

yes

#

btw the function works fine a few times

chrome skiff
tardy sage
#

then stops working for some reason

marble igloo
#

if the uidgenerator is invalid and part of the game object manager, the game object manager is invalid

tardy sage
#
GameObject(Game &game, int uid, std::string name, Vector3 pos = {0, 0, 0}) : _game(game), _uid(uid), _name(name)
    {
        transform.setPosition(pos);
    }
fossil sail
#

but yeah, if it works a few times then im 99% sure the manager is ๐Ÿ’€

tardy sage
#
    GameObjectManager _goManager;

It's also an instance in another class

marble igloo
#

sure, how did you create that

fossil sail
#

cause since it's all local variables the lifetime of them is linked to well the class they are contained in lifetime

tardy sage
#

it's part of the Scene class

    Scene(Game &game) : _game(game), _goManager(game) {}

marble igloo
#

sure, where is that scene created

tardy sage
#

which is contained in the game instance

    std::vector<Scene> _scenes;
marble igloo
#

and how is that populated

tardy sage
#
    template<class SceneC = Scene>

void addScene() {
        _scenes.push_back(SceneC(*this));
    }
marble igloo
#

SceneC?

tardy sage
#

its a template

marble igloo
#

and why is it a template and the member isn't

tardy sage
#

member as in the vector ?

marble igloo
#

cause your vector cannot store anything other than scene

fossil sail
#

also just for reference you got a complete callstack for us?

marble igloo
#

yes, the member vector

tardy sage
tardy sage
marble igloo
#

would be simpler to run the thing through a sanitizer or a debugger

marble igloo
#

I'm pretty sure you were told so already

tardy sage
#

probably

marble igloo
#

you are forced into using pointers

#

because the vector can only store base class instances

#

derived instances are sliced and essentially unusable

fossil sail
#

yeah....

tardy sage
#

I guess it doesnt make sense in my head

#

society just won't let me

#

it's right there just store it

fossil sail
#

no sly is correct this is broken... ๐Ÿค”

tardy sage
#

yeah they always are

fossil sail
#

you ever putting in another type of something that is not of type Scene? like a derived class?

marble igloo
#

an object is made of many parts

#

a derived object is made of the base object(s) and additional stuff on top of that

#

a vector only has enough space for exactly what you told the vector it had to store

tardy sage
#

I guess

marble igloo
#

hence the weird term "slicing"

tardy sage
#

Well help appreciated i'll go fix my stuff

marble igloo
#

because when you attempt to assign a derived object to a base object, the extra part from the derived object is dropped/discarded

fossil sail
marble igloo
#

never said slicing was the root cause of the segfault, but the design is just flawed

fossil sail
#

fair enough, I thought you where implying that

tardy sage
#

sanity check:

        _scenes.push_back(std::make_unique<SceneC>(*this));

That's the right way to make a unique_ptr and to store it into a vector of said pointers

fossil sail
#

well depending on the type of scenes yes

tardy sage
#
    std::vector<std::unique_ptr<Scene>> _scenes;
chrome skiff
tardy sage
#

I do not believe so

chrome skiff
marble igloo
#

unique_ptr to derived can be converted to unique_ptr to base so that's fine

fossil sail
#

this is somewhat fine it has a few issues though.... if there is a different constructor it will break, and if it's not a scene / derived type

marble igloo
#

but that's only for classes that derive from scene

tardy sage
#
   private:
    Game &_game;
    GameObjectManager _goManager;
marble igloo
#

and only if the base class has a virtual destructor

tardy sage
#

fields of Scene

marble igloo
#

reference data members don't sound like a great idea if you're not sure what you're doing

chrome skiff
fossil sail
#

what should be fine tehcnically I suppose. But still debetable design wise

marble igloo
#

I assume _scenes is part of Game and the *this is what you initialize the reference with?

fossil sail
#

I have some idea but ima need a callstack for this ๐Ÿค”
my guess would be there is some invalid pointer / reference to a scene somewhere where the call is generaterd

tardy sage
#

so I made a virtual scene destructor like sly said

class Scene {
   public:
    Scene(Game &game) : _game(game), _goManager(game) {}
    virtual ~Scene();
};
class RTypeGameScene : public Scene
{
public:
    RTypeGameScene(Game &game) : Scene(game)
     {
    }

    ~RTypeGameScene(){}

but I get
/usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o: in function Scene::Scene(Game&)':
/home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/Scene.hpp:13: undefined reference to vtable for Scene' /usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o: in function RTypeGameScene::RTypeGameScene(Game&)':
/home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/RTypeGameScene.hpp:12: undefined reference to vtable for RTypeGameScene' /usr/bin/ld: /home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/RTypeGameScene.hpp:12: undefined reference to Scene::~Scene()'
` at compile

fossil sail
#

did you implement it at all?

#

or do you only have a definition?

tardy sage
#

By all do you mean the RTScene destructor ?

#

cuz I modified the code to show the definition

fossil sail
#

Yeah, right now you only showing definitions, are they actually implemented anywhere? And yeah the destructors

tardy sage
#

well the implementation is the {} part

#

in my head atleast

fossil sail
#

Ah yeah my bad, missed that in the 2nd part, but the base?

tardy sage
#

I don't work with cpp files when figuring stuff out, makes it more cumbersome on the short term

fossil sail
#

ah allright ๐Ÿค” yes thats your issue then

tardy sage
#

well I didnt know it's needed

fossil sail
#

mark both of them as =default since they don't do anything

#

for constructors and destructors that don't do anything use = default

tardy sage
#

I have a similar pattern for my components and my base class's destructor isnt defined

fossil sail
#

well that is wrongish

#

cause you need to ensure the destructor of every object is properly called

tardy sage
#

well it still compiles

#

so i'm not getting the full story of why it doesnt compile on the scene

fossil sail
#

that doesn't mean it's correct ;)

tardy sage
#

(though implementing the base destructor did fix it )

fossil sail
#

cause in scene you mark it as virtual but the base class is never implemented

#

what here in this case is plain wrong

tardy sage
#
class IComponent
{
public:
    IComponent(GameObject &gameObject, std::string name) : _gameObject(gameObject), _name(name)
    {
    }
    virtual ~IComponent();
#

I have the same thing in my IComponent

#

that I store vectors of in the same way

fossil sail
#

do any other component actually implement a dtor?

tardy sage
#

no

fossil sail
#

there you go :)
propably why it compiles

tardy sage
#

If I remove my RTGameScene's destructor it still doesnt compile

fossil sail
#

why? well rather what error?

tardy sage
#

usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o: in function Scene::Scene(Game&)':
/home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/Scene.hpp:13: undefined reference to vtable for Scene' /usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o: in function RTypeGameScene::RTypeGameScene(Game&)':
/home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/RTypeGameScene.hpp:12: undefined reference to Scene::~Scene()' /usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o: in function RTypeGameScene::~RTypeGameScene()':
/home/aduthil/A3/B-CPP-500-PAR-5-1-rtype-maxime.eng/include/RTypeGameScene.hpp:9: undefined reference to Scene::~Scene()' /usr/bin/ld: CMakeFiles/r-type_client.dir/src/main.cpp.o:(.rodata._ZTI14RTypeGameScene[_ZTI14RTypeGameScene]+0x10): undefined reference to typeinfo for Scene'`

fossil sail
#

most likely cause the unique_ptr calls it :)

tardy sage
#

so I should have an implemented virtual destructor in the base class, and in the derived simply a destructor implementation ?

marble igloo
#

you have to define everything you declare

#

if there's nothing special to do you can use = default; as has already been mentioned

fossil sail
#

I think your base knowledge is lacking here:

#include <iostream>

struct base {
   virtual ~base() {std::cout << "~base\n";}      
};
struct derived : base {
   ~derived() {std::cout << "~derived\n";}        
};

int main()
{
    derived a;
}

~derived
~base
struct base {
   ~base() {std::cout << "~base\n";}      
};
struct derived : base {
   ~derived() {std::cout << "~derived\n";}           
};

int main()
{
    base* a = new derived();
    delete a;
}
~base
#include <iostream>

struct base {
  virtual ~base() {std::cout << "~base\n";}       
};
struct derived : base {
   ~derived() {std::cout << "~derived\n";}          
};

int main()
{
    base* a = new derived();
    delete a;
}
~derived
~base
marble igloo
#

I think you have typos in that last message

fossil sail
#
#include <iostream>

struct base {
  virtual ~base() {std::cout << "~base\n";}      
};
struct derived : base {
   ~derived() {std::cout << "~derived\n";}         
};

int main()
{
    base a = derived();
}
~derived
~base
~base

another fun example with slicing ;)

fossil sail
tardy sage
#

I'm not sure any of that made it any clearer

marble igloo
#

well, you still must define everything you declared

#

and you haven't done that

fossil sail
#

anyhow it would be good to carefully read those examples and their differences and their ouputs

marble igloo
#

the "undefined reference to vtable" is typically caused by you forgetting to define some of the virtual functions

#

which in short will be resolved if you actually defined everything properly

fossil sail
#

always implement declared function or mark them as =0

marble igloo
#

marking them as = 0; changes the semantics though

fossil sail
#

yeah, it does but I figured it might be a bit out of scope to explain that now ๐Ÿ˜…

marble igloo
#

I guess

fossil sail
#

but yeah, TLDR non implemented but declared functions are a ticking timebomb for compile errors

tardy sage
marble igloo
#

that's a weird terminology, usually it's declared but not defined

fossil sail
#

I always mix them up in my head

marble igloo
#

well you dereferenced a nullptr

tardy sage
#

oh I found my problem

#

thanks

fossil sail
#

the entire problem or just this?

deep pumiceBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.