Dereferencing Null Pointer

Pages: 123
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:

First-chance exception at 0x004e0ce6 in MyFirstApp.exe: 0xC0000005: Access violation reading location 0x70740724.
Unhandled exception at 0x004e0ce6 in MyFirstApp.exe: 0xC0000005: Access violation reading location 0x70740724.
quote]

and from the Stack Call:

[quote]
> MyFirstApp.exe!b2World::Step() + 0x6 bytes C++
MyFirstApp.exe!OnTimer() Line 691 C++
MyFirstApp.exe!OnRefreshMsgLoop() + 0x44 bytes
MyFirstApp.exe!_WinMain@16() + 0x1a4 bytes
MyFirstApp.exe!__tmainCRTStartup() Line 275 + 0x2c bytes C
MyFirstApp.exe!WinMainCRTStartup() Line 189 C
kernel32.dll!7c817077()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]


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
Access violation reading location 0x70740724.


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
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).
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:

If you can't see the bug in the lines of the code, you shouldn't look at your code.

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:

If you cannot find a bug with the debugger, you shouldn't use a debugger.

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:

If you look at the lines, you cannot see the lines missing.


If you debug, maybe the bug never occures during a debugging session or only at one time out of 10000 times. Maybe you have luck, but miss, that this would the bug, then could be, you will find this situation one year later, if you debug all days, but could be, you also miss again, that this would be the bug.


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
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()

1
2
DisableTimer();
EnableTimer();


Would this be sufficient or shall I tell where?
Last edited on
closed account (1vRz3TCk)
dadabe wrote:
If you can't see the bug in the lines of the code, you shouldn't look at your code.
but you need to look at the code to know that you shouldn't look at the code, so at some point while you are looking at the code to decide if you should be looking at the code a thought hit you. 'I should not be looking at the code ... but how will I know if I should have looked at the code...' <aneurism>


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.
@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
The best time is when you are sleeping!:)
closed account (4z0M4iN6)
vlad from moscow wrote:
it is a greate advice to programmers: close your eyes and meditate in the pose of lotus.:)

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
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.


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:


UsedByOnTimer = NewUsedByOnTimer;


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:

DisableTimer();
UsedByOnTimer = NewUsedByOnTimer;
EnableTimer();


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:
The best time is when you are sleeping!:)

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
closed account (1vRz3TCk)
dadabe wrote:
In b2World::Step() you will see something like:

UsedByOnTimer = NewUsedByOnTimer;


Why exactly would you set say something like that?
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
closed account (1vRz3TCk)
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
You aren't, by chance, destroying bodies while the step is happening?

http://www.iforce2d.net/b2dtut/removing-bodies
Pages: 123