typedef struct - lossing data on some levels at random times

Member
Posts: 39
Joined: 2008.10
Post: #1
Hey guys,

Been having this bug crop up in various forms but all with typedef struct

Basically im using it to store various data, such as level events and objects that need to be spawned and also for stats of a enemy. eg. enemyType[4].life is what i access when i create a new enemy of that type.

This works fine but recently its been returning 0 so the enemy just dies, but only sometimes on certain levels, its as if it randomly deletes the data because i only define it once at the initial game load. Its an int value.

Are there known issues with the data type or maybe a fix to force it to never remove that data until disposed of myself?

Let me know if you need any more information.

Thanks for any help Smile
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
No, you don't have to worry about typedefs loosing your data. However, in C there are a lot of ways to modify memory in C in unexpected ways. Here are just a few:
  • If you have a bad pointer that you are writing to somewhere in your program, you could be overwriting it with bad data.
  • If you are creating the struct on the stack (not a global variable or not allocated using malloc()), then your struct becomes invalid when the function it was created in returns.
  • You allocated the struct using malloc(), but continued to use it somewhere in your program after calling free().

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #3
Thanks for the information, should help.

Any advise on locating a bad pointer? How can i find if something is overwriting its location?
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #4
Could we see your code? This is kind of a hard problem to solve without looking.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #5
I understand, only problem is its a very big project with 20,000 lines or so... I dont know which section could be redefining the struct because I only ever access the data, but do that in lots of places.

I just made it malloc which hopefully will help.


One thing I have noticed is problems start to arise once a level is over a certain size - levelData is a struct that holds infomation on spawning levels - enemies, islands etc and when thats over 10 it seems to start causing issues, since I can make the level smaller and the issue goes away.

Could this be memory clean up that dumps the information?

Evidence of this is a NSLog caused a crash when tracing out the string of the last leveData and gave a memory location instead of the expected int value. But when I wrote it originally it traced out fine.

If you have an idea of something that might help you understand ask away, I realise im asking a lot but i really dont know what to show to help.
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #6
How is your levelData allocated?

About the best solution I can suggest is to check everywhere that levelData is being accessed and place checks on where you are accessing.

Also, check that all local variables are being initialized, I've had a lot of problems before with a local that was indexing an array and being incremented, but never initialized to 0 in the beginning.
Quote this message in a reply
Member
Posts: 39
Joined: 2008.10
Post: #7
Thanks for the feedback

Ill do another round of checking based off of it, ill let you know if I have any findings.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Template: createFramebuffer called several times? SparkyNZ 2 6,109 Apr 16, 2011 02:46 PM
Last Post: SparkyNZ
  Submitting Updates to iTunes Connect and Review Times Bersaelor 4 9,990 May 26, 2010 09:51 AM
Last Post: AnotherJake
  How to design short game levels vangelisfx 1 2,289 Dec 23, 2009 01:27 PM
Last Post: ThemsAllTook
  Long shader compile times on iPhone 3G S conal 9 6,704 Nov 20, 2009 03:54 AM
Last Post: OneSadCookie
  Downloading the same game multiple times EndeavorBros 0 2,691 Nov 17, 2009 12:57 AM
Last Post: EndeavorBros