Sign in to follow this  
kburkhart84

Verifying Validity of a pointer

Recommended Posts

I'm creating a wrapper dll for IrrKlang that will be used in Game Maker. Everything works fine. But, due to the nature of Gamemaker, from my "extended" code, and from my dll, I don't have access to the "pointers" the game is using. You can't pass arguments to functions by reference like in C++, rather only copies of the values. You can return values, which is how GameMaker has in its variables the C++ pointers to things from my dll.

What I'm doing is passing pointers to the ISound interface from IrrKlang, so you can have it in order to stop the sound later, when it is a looping background sound for example. This works fine. IrrKlang uses a simple reference counting system, so when I drop() the pointer, and IrrKlang is done with it, it is no longer a valid pointer. Usually, that is OK, and I myself understand this and wouldn't try to use the same pointer again. But I don't have access from "my" side of the code to make sure I invalidate the variable that the "user" side has, therefore they can try to use the pointer again, though it causes the whole game to freeze. What I want to do is somehow verify in my C++ dll in code whether the pointer is still valid or not. This means that it would still point to an ISound* interface. The problems of course is that I can't dereference the pointer trying any member functions of the ISound because that causes the freeze, assuming it no longer is a valid pointer.

I put this topic in general programming, because the solution likely is a general solution for any pointers, not specifically IrrKlang interfaces. If there is no way to do it, then I guess I will have to leave it up to the user to make sure not to use the pointers once they stop the sound. But since Game Maker tends to be used by younger less experienced programmers(not exclusively mind you) I wouldn't to be able to simply return an error code when this happens instead of having it freeze.

Any suggestions?? Thanks in advance.

Share this post


Link to post
Share on other sites
You could use a handle based system. Give the user integers, these integers can be used to do a lookup of an internal data structure to find the pointers. Integers can be tested for validity: if the handle doesn't exist, then it is old or invalid. This is how most OS kernels work, taking/returning opaque data which can be validated, preventing user programs from crashing or corrupting internal OS state.

Share this post


Link to post
Share on other sites
That had occured to me. My version would likely simply be a list of pointers that gets returned to my dll, and then the user gets the index of the pointer he wanted. Then I keep track in my list of when a sound gets stopped, and in my list/array whatever, I flag that index as invalid, and when the user tries to use the pointer by mistake, I simply return with an error code, rather than call the dll function.

This had also occured to me, but I though it may not be worth the time investment, and that maybe it would be better for the GameMaker users to start learning about this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this