zephir-lang / zephir

Zephir is a compiled high-level language aimed to ease the creation of C-extensions for PHP
https://zephir-lang.com
MIT License
3.3k stars 466 forks source link

Can not unset variable with lastest zephir #798

Closed loint closed 9 years ago

loint commented 9 years ago

I can not use unset function with variable.

OS: Centos Zephir: latest version (0.6.0a)

Zep code

public function configurePath(array raw_config, string path_app) -> array
{
        var key, value;
        array tmp = [];

        for key, value in raw_config["app"] {
            let tmp[key] = path_app . "/" . value ."/";
        }

        unset(key);

        let raw_config["app"] = tmp;
        return raw_config;
}

Compile message:

[root@ip-172-31-31-11 master]# zephir builddev
Zephir\CompilerException: Cannot use expression type: variable in "unset" in /home/loint/master/app/Config.zep on line 151

                unset(key);
loint commented 9 years ago

@ovr I'm very interesting with zephir and want to help zephir team to fix bug and implement more useful feature but I don't have much experience with zephir compiler. How I can start with it ?

steffengy commented 9 years ago

@loint I would just try to understand what goes wrong in a specific case. (Which requires reading a lot of code and much testing and debugging)

In that case it's simply that unset is not "meant" for normal variables: Unset is pretty much the "opposite" (in terms of action) as "isset"

Isset is defined (by the zephir specifcation) as: This operator checks whether a property or index has been defined in an array or object:

and while isset (more or less) works with variables, unset does not.

The usage of unset in your example code is also unnecessary, since the variable is not accessed after. (which makes the statement totally redundant as key is destroyed by the memory frame implementation anways after the function has completed)

loint commented 9 years ago

@steffengy My source code sometimes fail with Segment Fault (only "Segment Fault" without warning) with simple code such as declaring new variable :( . I think the problem is memory leak, so I want to unset some variables to reduce memory and I found unset function like I used in PHP but not.

How can I release variable memory in zephir and how can I debug if "zephir builddev" occurs with Segment Fault ?

steffengy commented 9 years ago

Grab a debugger such as GDB or the one of Visual Studio and analyze why it crashes. The problem here is that you should understand PHP and zephir internals, which require a lot of work to get into and understand some relations.

It's also a good idea to take a look at the generated C-Code before.

Usually these kinds of bugs also consume a lot of time figuring out why something goes wrong, so it's most of the time the opposite of trivial.

Most segmentation faults I fixed in zephir were caused by invalid pointers. (trying to write to NULL pointers and similar kinds of problems)

So I do not think that an unset implementation would help you with that.

Generally it's best if you try to consolidate your specific crash into a small sample of code and then step by step analyze what is the exact source of the crash.

loint commented 9 years ago

@steffengy thank you for your explanation but I haven't used gdb with zephir. Can you help me with some instructions, I had just worked with zephir command and optimizers to write C code.

steffengy commented 9 years ago

The problem here is, that thats a rather complex topic. You should probably google to get familar with it (using some tutorials or whatever) And even if you can use it, it only is a powerful tool to get behind a specific bug, but you still have to do the figuring-out part, which requires indepth knowledge. It's nothing I can shortly express here, it's something that requires a very large amount of work to get familiar with the codebases and internals aswell as the tools.

loint commented 9 years ago

Thank you very much, I understood.