| JonB (4) | ||
|
Hi all, first post, please be gentle! I am trying to write using Dragonfire SDK / C++ to create an iphone game. I am using Box2d as a physics engine. I keep getting the following error message at random times during the running of the App in the simulator:
After a lot of reading, I have concluded that I am dereferencing a Null Pointer and then trying to use it after this. However, nowhere in my code do I overtly set a pointer to 0 or NULL. I've read that return values from methods (particularly if using "if" statements) can set pointers to Null but I can't find example code of this. I'm reluctant to put my code up as I wouldn't know which bits to omit and it's therefore quite long! Any general answers about dereferencing Null pointers in this context would be much appreciated. Thanking you in advance, Jon | ||
|
Last edited on by admin
|
||
| Moschops (5956) | ||
Looks like you're trying to read memory location 0x70740724, and the operating system objects to this. If you were trying to dereference a null pointer, that would say location 0x00000000. | ||
|
|
||
| closed account (4z0M4iN6) | |
|
The error messages don't show anything about a null pointer. You should look at the beginning of your function b2World::Step(). It seems, you used a deallocated pointer or called a function, which returned a pointer to local data within the called function, and which now are destroyed and deallocated. Oh, look also to the parameters for function OnTimer(), which follows the call. The call could be also a parameter of function OnTimer(). The error is caused finally in function OnRefreshMsgLoop, maybe after one or too lines source code - I can't know it exactly, without seeing the code. Maybe also, the cause of the error isn't in Step(), but in OnTimer() or at last in OnRefreshMsgLoop(). | |
|
Last edited on
|
|
| Gaminic (1541) | |
|
It's not just about setting a pointer to 0/NULL. Any pointer that isn't assigned an address is automatically a nullpointer (or at least some type of bad pointer you shouldn't be dereferencing). You should check that every pointer gets assigned an actual address (new, copy from other pointer, &operator from other variable, ...). Also, make sure that any function returning pointers don't return a pointer to a temporary object (any object created in the function is going to die at the end of it). | |
|
|
|
| JonB (4) | |
|
Thank you to all. There's plenty there I can work on! Jon | |
|
|
|
| closed account (4z0M4iN6) | |||||
|
Hello Jon, I know now about the bug and I know the solution. This bug is one very interesting bug. It's a bug, most of the programmers will not find. I didn't see the code b2World::Step(), but the bug happens here. But if you look at this function and want to see the bug, you will not see it. You cannot see it in the lines of you code, because it's not inside the lines of your code. My first advice:
There are not much programmers, who know this And this is such a case, you can look as long you want, you will not see the bug. And I know professional programmers, they looked and looked and looked, without any result. What should you do then? Should you use a debugger. No, because: My second advice:
There are not much programmers, who know this And it's also such a case, you can debug as long as you want, you will not find the bug And I know professional programmers, they debugged and debugged and debugged, without any result. My explenations:
Can you gess it? What I saw clearly in my mind, without seeing your code: There are two lines missing in your function b2World::Step(). Can you guess? | |||||
|
Last edited on
|
|||||
| Gaminic (1541) | |
|
My first advice: Look at your code, not at dadabe's post. I'm pretty sure he's tripping balls. | |
|
|
|
| closed account (4z0M4iN6) | |||
|
@Gaminic Ok, you want it to know! How can you be sure, do you have any proof? Did you think it over and did you come to another conclusion? But your advice means: You don't know nothing! @JonB What would you think about these 2 lines in b2World::Step()
Would this be sufficient or shall I tell where? | |||
|
Last edited on
|
|||
| CodeMonkey (661) | |||
It all sounds very Zen, I order to find the bug you must not look for the bug. Or post the OnTimer() and b2World::Step() code and we can take a look. | |||
|
|
|||
| vlad from moscow (3105) | |
|
@dadabe, it is a greate advice to programmers: close your eyes and meditate in the pose of lotus.:) | |
|
|
|
| closed account (4z0M4iN6) | |
|
@CodeMonkey I never told, not to look at the code. I told, "if you can't see". This implies, first to look at the code and later to think it over. Don't forget to think it over. Good times for this are, on your way home from work. Or after you waked up. When you don't have any stress and can think without disturbence. Bugs, which were not simple ones, I solved on such occasions. | |
|
Last edited on
|
|
| vlad from moscow (3105) | |
| The best time is when you are sleeping!:) | |
|
|
|
| closed account (4z0M4iN6) | ||
vlad from moscow wrote:
If programmers mean, they would need a pose of lotus for thinking something, yes then they should do this. I never heard, thinking would be some rare skill, which only could won by zen. | ||
|
Last edited on
|
||
| moorecm (1802) | ||
Without much information to go on, it sounds like the object at that location had gone out of scope (likely a double-free situation). Either verify ownership/lifetime of your objects and/or run Valgrind on it. | ||
|
Last edited on
|
||
| closed account (4z0M4iN6) | |||
|
@JonB and @CodeMonkey In b2World::Step() you will see something like:
Whether it's one line or more, don't matter. A timer event may also occur during one line of source code. And when this happens, we have an undefined state. What would you think, would happen, if the assignment is only made half, half of some data? So the solution is:
And don't forget, the assignment is made at the begin of Step(). Should be the first line. | |||
|
Last edited on
|
|||
| closed account (4z0M4iN6) | ||
vlad from moscow wrote:
Exactly, you seem to know it. When we sleep, our brain works on the impressions, we had got during the day. And when we wake up, we know it. I see, you know! But this works only, if the sleeper has something like brains. And before we prepare to sleep, whe should have thought about the problem so that our brain can work on it, during we sleep. And exactly this way I found it. After I waked up, I sorted the thoughts, which were coming. And this is the way, how to debug the non trivial bugs. I think, this was a very interesting debugging case. | ||
|
Last edited on
|
||
| CodeMonkey (661) | ||||
| ||||
|
Last edited on
|
||||
| closed account (4z0M4iN6) | |
|
@CodeMonkey Why do you ask me? Can I know? I read only, that the bug occurs randomly, that there was a bug, because of the error message, and the first two error messages from the stack call and showed you what they mean. More I cannot tell, I'm not a clairvoyant. | |
|
Last edited on
|
|
| CodeMonkey (661) | |
| I ask you because you are the one that said it. Why do you think that there is a line like that at the start of b2World::Step()? | |
|
Last edited on
|
|
| cire (1845) | |
|
You aren't, by chance, destroying bodies while the step is happening? http://www.iforce2d.net/b2dtut/removing-bodies | |
|
|
|