Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / TIER 2 : Inserting a new element into unordered_map or map give access violation error

Author
Message
nickydemon
8
Years of Service
User Offline
Joined: 19th May 2015
Location:
Posted: 28th Jul 2020 16:56 Edited at: 28th Jul 2020 16:57
Hi, I am currently using tier 2 of AGKS. I am trying to implementing a simple scene system by referencing this guy tutorial :
http://thatgamesguy.co.uk/cpp-game-dev-6/

I have done this in another library like raylib and it works well. But in app game kit tier2, it gave me access violation error whenever I try to insert new element in unordered_map or map.
For example, I create an unorder_map in the class app in template.h and insert new element in begin() method in template.cpp



After executing it just showed that I have access violation. To be honest I have no idea what is happening. Am I doing something wrong?
Xaron
9
Years of Service
User Offline
Joined: 3rd May 2014
Location: Germany
Posted: 29th Jul 2020 07:57
Can you show a bit more of the code? Where is _test created, what is it?

Beside that it's often better to use "emplace" instead of "insert" nowadays.
nickydemon
8
Years of Service
User Offline
Joined: 19th May 2015
Location:
Posted: 29th Jul 2020 08:07
Hi Xaron, I am sorry that I provided too less of my code. The _test variable is created inside template.h



Below is the error showed when in debugging mode. Please refer attached file. It's in japanese and the message just stated that An exception has been thrown due to access violation.

Attachments

Login to view attachments
Xaron
9
Years of Service
User Offline
Joined: 3rd May 2014
Location: Germany
Posted: 29th Jul 2020 08:21
There seems to be a problem using a map or unordered_map directly it seems.

Just declare it like that in the header:


And then create it properly in the cpp:
nickydemon
8
Years of Service
User Offline
Joined: 19th May 2015
Location:
Posted: 29th Jul 2020 09:21
Hi Xaron, it works now! Thank you for the solution.
But may I know what's the cause of the problem behind this? It worked fine if I use the other library though..
Xaron
9
Years of Service
User Offline
Joined: 3rd May 2014
Location: Germany
Posted: 29th Jul 2020 09:53
I have no idea actually. It could be that the map isn't properly initialized yet, but I don't know why that should happen there. Maybe due to static linking to the agk lib and weird internal order of initialization? Don't know.
nickydemon
8
Years of Service
User Offline
Joined: 19th May 2015
Location:
Posted: 29th Jul 2020 10:02
I see. I guess I will just use the solution you provided.
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 29th Jul 2020 14:57
It is probably the "memset ( this, 0, sizeof(app));" line, this is setting all the private variables to 0, including the internal variables in the unordered_map. It was originally intended to initialize basic types to 0, but is probably not a good idea when using more complicated types.
basjak
14
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 1st Aug 2020 03:09 Edited at: 1st Aug 2020 03:47
it obviously the memset. you need to remember that you can't set the 'this' with memset because the object heap access is compiler protected. the 'this' can only be set by accessing its variables and only be deleted either automatically if it is on the stack or through the pointer delete if it is on the heap.

Login to post a reply

Server time is: 2024-04-20 08:30:22
Your offset time is: 2024-04-20 08:30:22