PHP Extension: MAKE_STD_ZVAL clobbers my variable!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lambart
Forum Newbie
Posts: 4
Joined: Thu Dec 05, 2002 2:28 pm
Location: La Ciudad de Santa Cruz

PHP Extension: MAKE_STD_ZVAL clobbers my variable!

Post by lambart »

Hi. I'm writing a PHP extension... if you don't know anything about extensions, this will probably be very confusing to you.

OK, so I've got a variable called retVal, which is a pointer to a structure. I'm allocating the variable (using emalloc as recommended by Zend) in the same way I do all over the place, and it always works fine, except in this one function.

When I get to the end of the function in question, I've figured out that retVal is pointing to the same memory location that is used for a zval* I've got!

It took me a while to figure out where the problem was.

I allocate and initialize retVal first, and everything is just fine until I try to initialize the zval* (named theList) using the Zend MAKE_STD_ZVAL() macro. After MAKE_STD_ZVAL() is done, I check the address of retVal and theList, and they are pointing to the same exact address in memory!! Before that, theList was NULL.

here's what's basically happening:

Code: Select all

variant* retVal = NULL;
zval* theList = NULL;
/* emalloc and init the variant */
retVal = new_variant(VT_NULL, NULL);
/* OK, retVal now has a valid address and valid struct members, 
 * verified by using printf("%p", retVal) or examined in gdb 
 *
 * NOW, allocate the zval
 */
MAKE_STD_ZVAL(theList)
/* theList now points to retVal's memory location! */
I've spent a while looking at the source for zend_alloc.c, zend_fast_cache.c, etc. etc. and really can't figure out what's going on.

The worst part is that the address of the zval is supposed to get assigned to a member of the struct pointed to by retVal, so it turns into a real mess.

I could try allocating/initing retVal AFTER doing the zval, but why should I?

Does this look like I've found a bug in Zend, or am I doing something really stupid? I am working with PHP 4.2.3, by the way.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

very hard to say from the code snippet ;)
lambart
Forum Newbie
Posts: 4
Joined: Thu Dec 05, 2002 2:28 pm
Location: La Ciudad de Santa Cruz

code snippet

Post by lambart »

Yes, well... I realize that. I also don't think you'd be wanting to look at all my code, so I tried to distill it down to the minimum to explain what is happening. I'm not (at this point, at least) looking for someone to spend a lot of time trying to understand the rest of the code so they can tell me "this is what you're doing wrong"...

I'm more interested in knowing if anyone has seen such behaviour before, or any misbehaviour of emalloc() at all. I certainly wouldn't suggest I'm the mythical "infallible programmer"... but I don't understand how Zend could ever allocate a memory address that is already allocated to another variable, unless there's a bug. I am absolutely not (e)free'ing the retVal before calling MAKE_STD_ZVAL() so it has no business using retVal's address. They are not even related data types!

So...
(a) has anyone ever seen this sort of behaviour before (even outside of Zend... have you seen this while doing any C programming using the standard malloc/calloc/free/strdup/etc. functions)?
(b) if not, does it sound like there's a good chance this could be a bug in Zend?

If it is a bug, I just can't see how it would only be affecting me... so for now I'll go back to trying to figure out what I could be doing wrong.

Über eine hilfreiche Antwort würde ich mich trotzdem riesig freuen :)
lambart
Forum Newbie
Posts: 4
Joined: Thu Dec 05, 2002 2:28 pm
Location: La Ciudad de Santa Cruz

Followup: Problem Disappeared

Post by lambart »

...of course, when things start getting really weird, doing a "make clean" is always worth a try :?

I have no idea what was happening, but it seems to be gone now!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:D
Post Reply