Jump to content
  • Advertisement
Sign in to follow this  
Goober King

Memory errors

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been trying to breath some life into a floundering project of mine. The main obstacle in my way has been a memory access error I haven't been able to correctly identify. I am fully aware of the lines of code triggering the violation and occasional crash, what I haven't been able to figure out is why what its accessing isn't valid. So what I think I need to do first is get my C 101 checked by better coders. Mostly I want to get my logic checked. By that I mean I need to find out if what I'm trying to do is correct or if I'm going about things in the wrong way, flaw in the code or flaw in the logic. So here's what I'm doing. The game is a 2D Fighter( 2.5D if you will.) At current the game has functioning title screen, character select screen and a very very early main game. Initially the game runs fine, however the problem shows up if you leave the main game. When your return to the game, as if you were moving on to next match or just picked a new fighter, the game will often crash during the next fight. The crash comes from the animation table being linked to information freed from the previous match. At this point it should be looking at only fresh data but obviously something is wrong. I am missing something or am doing something in the wrong way. To get more technical here is basically how things work. After a character is selected the program first loads the character package. This package is created with an external editor that list the image files used for the character, knows how each frame is built up from sprites, how the character animates, and describes the collision zones for the character. All this data is loaded into a Player data structure that holds all this information as well as player keys, current location, and anything and everything else about that player. All the visible animation is handled through an animation table. This table is a list of all the currently playing animations. When a player jumps, kicks, or whatever that animation sequence gets posted on the table. The system then processes the list animating each member in various ways. Each entry in this "animation table" has a flag that says if it is a current animation or a past/dead animation that can be ignored by the processing functions. After an animation dies the flag is changed and the old data lingers. However every new animation must be registered and initialized completely for it to function correctly even once. Here is where things get confusing to me. Each entry in the animation table has a pointer to the original owner's structure where the actual animation data is stored. After a fight has been canceled all the stored/malloc'ed information is released using free(). The actual structure remains and is reused. As for the animation table every animation is flagged as dead, but the old data is still there. When the next match is loading the new player package is loaded into the structure and everything is reinitialized exactly as it was done the first time around. The crashes happen during the second or later matches when an animation is trying to be displayed. The draw function goes to look up the image data from the source listed on animation table and sometimes crashes. A Memory leak program shows it as looking up previously freed data even though everything listed on the table should be initialize and pointing to only fresh data. Since I haven't yet found any coding flaw in what I'm trying to do, It is leading me to suspect my understanding of what I should and can do is flawed. The fact that everything works great the first time and only freaks out later also leads me to think that I'm mismanaging my memory. Anyway, if anyone has any idea what direction I should be looking in I would love to hear it.

Share this post


Link to post
Share on other sites
Advertisement
After you are freeing your memory, what are you doing with your pointers? If you are not setting them to NULL then this would cause memory leaks. Even if you free the memory if you do not initialize the values to something right away, and then try to initialize them later, you will get memory leaks.


Here's an article on the issue.
http://www.eskimo.com/~scs/cclass/int/sx7.html

Share this post


Link to post
Share on other sites
DecipherOne, that is totally confusing the way you worded it.
Better to say that if you are using the if ( p ) construct to test if a pointer is pointing to good memory
then you also have to set p=NULL after freeing it, or your allocation test will fail. This isn't a memory leak.
A leak is an allocation with no associated free.

GooberKing, things you make want to look into would be what decipherone was talking about, where in you
didnt mark something as free, when you thought it was.
You may also want to look up smart pointers, they'd make your life a bit easier.
And lastly, you should make sure you are using refrences and not values, or you
could be in a situation
where you think the structure changed, but it didn't.
ie. ClearAnimation ( Animation &x ) instead of ClearAnimation ( Animation x )
And so even though you did do p = NULL, that change didnt propagate.

Share this post


Link to post
Share on other sites
Quote:
Original post by DecipherOne
After you are freeing your memory, what are you doing with your pointers? If you are not setting them to NULL then this would cause memory leaks. Even if you free the memory if you do not initialize the values to something right away, and then try to initialize them later, you will get memory leaks.


Here's an article on the issue.
http://www.eskimo.com/~scs/cclass/int/sx7.html


Well no I haven't been doing that. I think there are some areas that could use a look over in that regard. Won't be able to do it right away but I will see about adding a few more checks and seting stuff to NULL.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
DecipherOne, that is totally confusing the way you worded it.
Better to say that if you are using the if ( p ) construct to test if a pointer is pointing to good memory
then you also have to set p=NULL after freeing it, or your allocation test will fail. This isn't a memory leak.
A leak is an allocation with no associated free.


I guess I did word that kind of funny. You have to forgive me, I've been knocking my head against a wall the last day trying to solve an issue of my own. And I'm kinda burnt out. I think that the solution was still presented though, which should account for something I think. At any rate, the pointers are no longer valid and are no longer use able by the program.

Share this post


Link to post
Share on other sites
One other thing that it could be is deep copy versus shallow copy is there anywhere tht you pass structs around rather then pointers, if so consider weather the data that any of the pointers the struct contains needs to be copied as well or if its okay for the two structs to point to the same data....

Id suggest writting copy ctors and assingment operators but it sounds like your using straight C so its probaly going to be hard to find where your doing it if it is a shallow copy vs deep copy problem....

Share this post


Link to post
Share on other sites
Please re-explain (and post code) about what is free'd at a fight end and how the structures are re-used.

Oh, and since you're using C, it might be the ever present buffer overflow in some random other char*/array assignment making your pointers point randomly...

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Please re-explain (and post code) about what is free'd at a fight end and how the structures are re-used.

Oh, and since you're using C, it might be the ever present buffer overflow in some random other char*/array assignment making your pointers point randomly...


Well the inital point of the post was to check my logic more so than actual code. In the intrest of clarity I will try to pseudocode over the actual. I can get the actual if it is needed but I'm about to race off to work and it will have to wait

The main player structure is mostly strait vars that are for this and that. The part that must get freed is in the animation data, Which is actualy a somewhat complicated nest of structures. The main player has an array of stuctures that define indivitual animation "sequences" A kick, a jump, a walk, ect. Each sequence has 4 pointers. So each sequence must be freed. After the fight is canceled each of the sequences must be checked and freed if they were being used. So 150*4 pointers freed.
Out of time, Doubt that helps but there it is.

Share this post


Link to post
Share on other sites
So, you have a bunch of pointers to structs... you free them, and then wonder why re-using the free'd structs leads to a crash? Forgive me if that's a little blunt, but that's what you're describing.

Post real actual code. Yes, it might be ugly and convoluted. Everyone has ugly and convoluted code somewhere.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
So, you have a bunch of pointers to structs... you free them, and then wonder why re-using the free'd structs leads to a crash? Forgive me if that's a little blunt, but that's what you're describing.

Post real actual code. Yes, it might be ugly and convoluted. Everyone has ugly and convoluted code somewhere.


No that's not what I'm doing. At least not intentionaly. I wouldn't honestly expect that to work. The structures are never freed. Only a set of pointers with in them pointing to int's and one to a char array.

Before I get into the source code I want to look over it again and add a few more checks and see what happens. If I can't figure it out I will come back with code in a few days and everyone can take another crack at it.
Thanks for everyone's input.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!