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.

Dark GDK / Inventory vector problem

Author
Message
NoviceNate333
11
Years of Service
User Offline
Joined: 7th Dec 2012
Location: United States
Posted: 4th Feb 2016 11:14
So i am currently working on an rpg game... of course lol. I am having some trouble with the inventory class. The inventory has a max size of 20. in the class when i click the item in my inventory it works most of the time sometimes when i click the last item i get an error and im not sure why i think its an access violation but its not giving me that error. here is my entire class.

This is the error i get from the vector class


Im not really sure what to do from here
here are some pics to whow what happens

Attachments

Login to view attachments
The Tall Man
11
Years of Service
User Offline
Joined: 16th Nov 2013
Location: Earth
Posted: 5th Feb 2016 19:49
Since I don't use Microsoft's STD stuff, I'm not familiar with Vector. But if you believe you're getting a memory leak, it would seem that your item class would be the relevant code to look at.

However, from what I saw in your Add function, a couple things stood out to me that ~seem~ unrelated:
1. When you compare the item names, what you're doing is comparing the char[20], in other words, char*... you're comparing pointers, not content. Since you're copying the name when you add an item, that comparison of names will never return true. The same goes for your comparison of types.
2. When you added the new item, I didn't see anywhere, anything that would increment items.size.

When you correct those issues, it's possible your other issue will vanish. Usually a memory leak has a specific error though, not a general one like you're getting. If you set a breakpoint at the line of code that triggers the error, just before the error occurs, you may find that _Pos or size() are NULLs. In other words, like calling a class function to an unallocated class. That's just a guess.
Judging what we see is the greatest blinder and self-limiter in the universe.

What we perceive is never reality. It is only a story we tell ourselves based on our current perspective, which has far more to do with our beliefs about ourselves than with anything else.
The Tall Man
11
Years of Service
User Offline
Joined: 16th Nov 2013
Location: Earth
Posted: 5th Feb 2016 19:53
Oops... I didn't realize items was a STD::Vector type, when I said it would be helpful to see its implementation.... which brings me back to my unfamiliarity with STDs.
Judging what we see is the greatest blinder and self-limiter in the universe.

What we perceive is never reality. It is only a story we tell ourselves based on our current perspective, which has far more to do with our beliefs about ourselves than with anything else.
The Tall Man
11
Years of Service
User Offline
Joined: 16th Nov 2013
Location: Earth
Posted: 5th Feb 2016 20:20
Okay... I'm starting to understand your code a bit more now, and your use of Vector. You're allocating space for a new item, then storing pointers to that item in the Vector list. When you drop an item, you're not deallocating the memory for that item, once you've dropped it from your vector list. And here's where I think your main problem is: It's hard to tell because you don't have your brackets lined up, but it looks like you're looping through ~all~ your items when you drop, so you're dropping them all, not just one. It looks like you're working on the selected item when drop is false, but bypassing that item selection block and dropping them all when drop is true. Best way to tell is to first, line up your brackets, and secondly step through (debug) the code.
Judging what we see is the greatest blinder and self-limiter in the universe.

What we perceive is never reality. It is only a story we tell ourselves based on our current perspective, which has far more to do with our beliefs about ourselves than with anything else.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 17th Feb 2016 01:40 Edited at: 17th Feb 2016 01:41
There are a lot of things wrong with your code.


This code isn't comparing strings. You're comparing string pointers rather than the content of the strings. Instead of items[i ]->type != "Weapon" you need to use strcmp(items[i ]->type, "Weapon") != 0). That, or use std::string instead of char[30]:


Same here:



Interestingly, you're doing it correctly later on:


Next, this here:


1) You're not calling delete on the item you're erasing -> memory leak. Should be:


2) When you call items.erase(items.begin() + i) you're modifying the container. All proceeding items in the vector are shifted down by 1. This means that the variable i is indexing one item later than it should be, which will give you an out of bounds error when you reach the last item.

Don't use for(int i = 0; i != items.size(); i++). Use iterators.

"It also shoots blue flames sometimes, which is startling in the most exquisite of ways." -- Not me
Hotline
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 17th Feb 2016 02:57 Edited at: 17th Feb 2016 03:03
1. Use std::string instead of char array
2. It's absolutely unnecessary to allocate "Items" dynamically.
3. A stated above , use iterators.
4. Since you iterate through the whole vector of items in your code each frame anyway , there are no benefits using std::vector in this case. Use std::list for faster insertion and removal of items
[href=forum.thegamecreators.com/?m=forum_view&t=191567&b=5]Spark Particle engine[/href]
[href=forum.thegamecreators.com/?m=forum_view&t=199163&b=5]Transform gizmo plugin[/href]
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 17th Feb 2016 10:52
Quote: "4. Since you iterate through the whole vector of items in your code each frame anyway , there are no benefits using std::vector in this case. Use std::list for faster insertion and removal of items"


He's only got 20 items, std::vector is going to be a lot faster than std::list because it will have less cache misses.

http://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
"It also shoots blue flames sometimes, which is startling in the most exquisite of ways." -- Not me
Hotline
16
Years of Service
User Offline
Joined: 22nd Aug 2008
Location:
Posted: 17th Feb 2016 16:13
Quote: "He's only got 20 items, std::vector is going to be a lot faster than std::list because it will have less cache misses."


i didn't examined his code that deep
[href=forum.thegamecreators.com/?m=forum_view&t=191567&b=5]Spark Particle engine[/href]
[href=forum.thegamecreators.com/?m=forum_view&t=199163&b=5]Transform gizmo plugin[/href]

Login to post a reply

Server time is: 2024-11-16 16:23:30
Your offset time is: 2024-11-16 16:23:30