0

I am working on a rendering framework which has been progressing quite nicely. The framework is compiled into a static library. To test it I have made a different project that links the library files and creates some different scenes and such. Recently I decided that I wanted to be able to render Fbx meshes so I downloaded the Fbx SDK and created a new project where I played around with it to make sure I got the linking and functionality to work in a bare minimum setting. Now that I have got a working prototype I added it to my testing project (by linking the static library) and added the code. My project compiles fine, but I have run into a strange problem.

Seemingly if there is any usage of the Fbx SDK code in my code (no need to call/reach it, just that it is there) I get a bad alloc error from an unordered_map in my rendering framework (the unordered map is a static member of a class if that is in anyway relevant) once the first insertion into the map is attempted. If I remove the Fbx code (as in, don't use the code in the SDK, not that I actually remove the SDK from the project) then I do not get the bad alloc error. I assume I have done something whacky, but I have not found any evidence of what it could be. I have been using Visual Studio 2017 for all of the projects. All help is appreciated.

Edit: just discovered something weird, the problem only seem to appear in debug x64, not in release x64/x86 or in debug x86

Foo.h

class Foo
{
private:

    static std::unordered_map<std::string, size_t> ids;
    size_t myID;

public:
    Foo();
    Foo(const std::string& identifier);

    ...
};

Foo.cpp

std::unordered_map<std::string, size_t> Foo::ids;

Foo::Foo()
{
    myID = 0;
}

Foo::Foo(const std::string& identifier)
{
    auto target = ids.find(identifier);

    if (target == ids.end())
    {
        myID = ids.size() + 1;
        ids[identifier] = myID;
    }
    else
    {
        myID = ids[identifier];
    }

}
Gamewolf3000
  • 63
  • 2
  • 8
  • You mentioned that the `unordered_map` is `static`; does this possibly get used across different threads without synchronization? If this is possible, you might just be getting undefined behavior where the behavior happens to throw a `bad_alloc` – Human-Compiler Sep 06 '20 at 15:09
  • Sounds like a [static initialisation order problem](https://stackoverflow.com/questions/1005685/c-static-initialization-order). I've sometimes seen linking of unrelated code expose this issue. – john Sep 06 '20 at 15:15
  • This question fails to show anything that will meet stackoverflow.com's requirements for a [mre], and because of that it is unlikely that anyone here can determine the problem, at most any response will be a random guess as to what the issue is. This question must be [edit]ed to show a minimal example, no more than one or two pages of code (the "minimal" part), that anyone can cut/paste, compile, run, and reproduce the described problem (the "reproducible" part) ***exactly as shown*** (this includes any ancillary information, like the input to the program). See [ask] for more information. – Sam Varshavchik Sep 06 '20 at 15:15
  • 1
    @SamVarshavchik To be fair, if it is a static initialisation order issue, it would be impossible to produce a [mre]. But some sample code would help. – john Sep 06 '20 at 15:17
  • It is very much possible to produce a [mre] consisting of two translation units with an obvious initialization order dependency. At least someone attempting to reproduce this will win the lottery and hit the bug, and thus be able to track it down. – Sam Varshavchik Sep 06 '20 at 15:22
  • I have tried to add the minimal amount of code that is relevant. The error occurs the first time an object of the class is constructed with the non default constructor – Gamewolf3000 Sep 06 '20 at 15:59
  • @Human-Compiler in this case there is no threading happening – Gamewolf3000 Sep 06 '20 at 16:01
  • @Gamewolf3000 the question in, when exsctly that hsppens? If consructor called from another static initialization (i.e. before main() starts), it is possible that constructor of foo() is called too early. – Swift - Friday Pie Sep 06 '20 at 18:45
  • @Swift-FridayPie The constructor is called during the execution of the main, I can step with it just up to the point where it tries to insert into the map without any problem until that point – Gamewolf3000 Sep 06 '20 at 21:45
  • Well, then the only remaining variant is that problem is happening somewhere before. the problem depebding on target word size suggests that it is something related to different sizes of certain types. Also, do you link with correct binary of library? – Swift - Friday Pie Sep 07 '20 at 03:45

0 Answers0