can you help a new pcre user???

N

news

Hi all,

I am a beginner on 'c' and pcre...and I am on windows 2000 and VC6
with all of the patches, etc.


The following program leaks lots of memory and I only make 1 pcre call.
I read the docs, but maybe I missed some cleanup or other calls???
Or maybe I messed up passing strings around???

All it does is call pcre_compile(). If I comment that line out no
memory is leaked. I have tried 2 versions of pcre, one from the
GNU link and the other was 4.1 on the contrib area. (from www.pcre.org)


// pcreleak.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <pcre.h>


BOOL ParseCmd(const char* pszPattern, const char* pszSubject, char*
pszResults);

const char* grxFullCmd = "\n(.*?)<<END>>";

int main(int argc, char* argv[])
{
char szResults[1024] = "";
char szCmd[] = "name=name1\nhost=host1<<END>>";

for (int i = 0; i < 500; i++)
{
int nRet = ParseCmd(grxFullCmd, szCmd, szResults);
Sleep(200);
printf("line %d\n", i);
}
return 0;
}


#define OVEC 3
#define OVECCOUNT (10*OVEC)
BOOL ParseCmd(const char* pszPattern, const char* pszSubject, char*
pszResults)
{
const char* error;
int erroffset;
int ovector[OVECCOUNT];

// NO leaks if we return...
return 1;

//extern pcre *pcre_compile(const char *, int, const char **,
// int *, const unsigned char *);
pcre* re = pcre_compile(pszPattern, PCRE_DOTALL, &error, &erroffset,
NULL);
if (re == NULL)
{
printf("PCRE compilation failed at offset %d: %s\n", erroffset,
error);
return -1;
}

return 1;
}
 
B

boa

news said:
Hi all,

I am a beginner on 'c' and pcre...and I am on windows 2000 and VC6
with all of the patches, etc.


The following program leaks lots of memory and I only make 1 pcre call.
I read the docs, but maybe I missed some cleanup or other calls???

You call pcre_compile() 500 times, and that function returns a pointer
to a memory block allocated with pcre_malloc(). Maybe you should call
pcre_free() to free the memory as well? ;-)

HTH
boa@home

Or maybe I messed up passing strings around???

All it does is call pcre_compile(). If I comment that line out no
memory is leaked. I have tried 2 versions of pcre, one from the
GNU link and the other was 4.1 on the contrib area. (from www.pcre.org)


// pcreleak.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <pcre.h>


BOOL ParseCmd(const char* pszPattern, const char* pszSubject, char*
pszResults);

const char* grxFullCmd = "\n(.*?)<<END>>";

int main(int argc, char* argv[])
{
char szResults[1024] = "";
char szCmd[] = "name=name1\nhost=host1<<END>>";

for (int i = 0; i < 500; i++)
{
int nRet = ParseCmd(grxFullCmd, szCmd, szResults);
Sleep(200);
printf("line %d\n", i);
}
return 0;
}


#define OVEC 3
#define OVECCOUNT (10*OVEC)
BOOL ParseCmd(const char* pszPattern, const char* pszSubject, char*
pszResults)
{
const char* error;
int erroffset;
int ovector[OVECCOUNT];

// NO leaks if we return...
return 1;

//extern pcre *pcre_compile(const char *, int, const char **,
// int *, const unsigned char *);
pcre* re = pcre_compile(pszPattern, PCRE_DOTALL, &error, &erroffset,
NULL);
if (re == NULL)
{
printf("PCRE compilation failed at offset %d: %s\n", erroffset,
error);
return -1;
}

return 1;
}
 
N

news

You call pcre_compile() 500 times, and that function returns a pointer
to a memory block allocated with pcre_malloc(). Maybe you should call
pcre_free() to free the memory as well? ;-)
Thanks, I see that now...I was following the sample program and it
never calls pcre_free() so I wasn't sure what was going on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top