Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory leaks &引用;“压缩泄漏”;固定,但不理解_Memory Leaks_Xpressive - Fatal编程技术网

Memory leaks &引用;“压缩泄漏”;固定,但不理解

Memory leaks &引用;“压缩泄漏”;固定,但不理解,memory-leaks,xpressive,Memory Leaks,Xpressive,我知道。Xpressive(可能)在这里没有错,但我花了很多精力查找内存泄漏,我不得不调整代码布局以修复出血 有人能给我解释一下为什么布局的改变能解决这个问题吗?我不明白为什么(正确/改进)使用“static const”可以修复漏洞 顺便说一句,泄漏发生在MIPs内核上,使用boost版本1.49,并与GCC 4.3.3交叉编译 原始“筛”代码: //source.cpp #包括> icase(“名称:”)>> (name_t=Xpr::token)>>eos; cmatch什么; bool

我知道。Xpressive(可能)在这里没有错,但我花了很多精力查找内存泄漏,我不得不调整代码布局以修复出血

有人能给我解释一下为什么布局的改变能解决这个问题吗?我不明白为什么(正确/改进)使用“static const”可以修复漏洞

顺便说一句,泄漏发生在MIPs内核上,使用boost版本1.49,并与GCC 4.3.3交叉编译

原始“筛”代码:

//source.cpp
#包括>
icase(“名称:”)>>
(name_t=Xpr::token)>>eos;
cmatch什么;
bool ok=regex_搜索(开始、结束、什么、regx);
...
返回ok;
}

每次打电话给foo时,似乎都会发生泄漏

编辑:在写了下面的回复后,我试图重现您的问题,但无法解决。这是我正在使用的代码

#include <boost/xpressive/xpressive.hpp>
using namespace boost;
using namespace xpressive;

cregex token = keep(+set[ alnum|'!'|'%'|'_'|'*'|'+'|'.'|'\''|'`'|'~'|'-']);
//cregex more  = ...

bool foo(const char * begin, const char * end)
{
    mark_tag name_t(1);
    cregex regx = bos >>
        icase("name:") >>
        (name_t= token) >> eos;

    cmatch what;
    bool ok = regex_search( begin, end, what, regx );
    //...
    return ok;
}

int main()
{
    char const buf[] = "name:value";
    while(true)
        foo(buf, buf + sizeof(buf) - 1);
}
#包括
使用名称空间boost;
使用命名空间xpressive;
cregex token=keep(+set[alnum |'!'|'%'|'|'|'*'|'+'|'。|'\''|'~'|'-');
//cregex more=。。。
bool foo(常量字符*开始,常量字符*结束)
{
标记标签名称(1);
cregex regx=bos>>
icase(“名称:”)>>
(名称=令牌)>>eos;
cmatch什么;
bool ok=regex_搜索(开始、结束、什么、regx);
//...
返回ok;
}
int main()
{
char const buf[]=“名称:值”;
while(true)
foo(buf,buf+sizeof(buf)-1);
}
这个代码不会泄漏。您是否可能正在使用xpressive的早期版本?你能发布一个完整的,独立的例子,这样我可以调查吗?更好的是,提交一个bug并将代码附加到那里。谢谢

埃里克

-----开始原始响应-----

我怀疑你和xpressive有冲突。有关在函数本地对象中嵌套全局正则表达式对象的警告,请参阅。我认为发生的情况是,为了防止挂起引用,函数local
regx
必须保留对
token
的引用,而
token
必须保留对
regx
的(弱)引用。正在增长的是
令牌的弱引用映射。从严格的技术意义上讲,这并不是一个泄漏,因为当
令牌被销毁时,内存将被回收。但这显然并不理想

不久前,我在xpressive中修复了一个类似的“漏洞”,添加了一个对映射的机会主义清除,以清除对过期正则表达式的弱引用。我必须调查一下为什么这件事没有发生。请提交一个bug。谢谢


同时,您的修复非常好。声明函数local
regx
static意味着它只会被构造一次,因此
token
的弱引用映射将永远不会超过大小1。

感谢您花时间响应。如果我发布真实的代码,我的雇主可能不会对我印象太深,对不起。经过进一步测试,发现即使在函数“foo”中使用static const也不足以完全阻止泄漏。但是我还没有看到Xpr类静态常量成员有任何泄漏。我的怀疑正朝着MIPS版GCC中的一些“搞笑”方向发展。我也不能复制“偏离目标”
// header.hpp
#include <boost/...

class Xpr {
public:
    static const cregex token;
    static const cregex more;
};

// source.cpp
#include "header.hpp"

const cregex Xpr::token = keep(+set[ alnum|'!'|'%'|'_'|'*'|'+'|'.'|'\''|'`'|'~'|'-']);
const cregex Xpr::more  = ...

bool foo(const char * begin, const char * end, size_t& length, std::string& dest)
{
    mark_tag name_t(1);
    static const cregex regx = bos >>
        icase("name:") >>
        (name_t= Xpr::token) >> eos;

    cmatch what;
    bool ok = regex_search( begin, end, what, regx );
    ...
    return ok;
}
#include <boost/xpressive/xpressive.hpp>
using namespace boost;
using namespace xpressive;

cregex token = keep(+set[ alnum|'!'|'%'|'_'|'*'|'+'|'.'|'\''|'`'|'~'|'-']);
//cregex more  = ...

bool foo(const char * begin, const char * end)
{
    mark_tag name_t(1);
    cregex regx = bos >>
        icase("name:") >>
        (name_t= token) >> eos;

    cmatch what;
    bool ok = regex_search( begin, end, what, regx );
    //...
    return ok;
}

int main()
{
    char const buf[] = "name:value";
    while(true)
        foo(buf, buf + sizeof(buf) - 1);
}