Quote:
> i've wrote a bookmark file content reader function to extract the URL from a
> bookmark file
> & the code is
> ////////////////////////////////////////////////////////
> void Cpopupdiag::ReadFileContent(CString filename)
> {
> char *urlstring=(char*)malloc(300);
allocates 300 characters, fine, but you don't need the cast to (char *),
just char *urlstring= malloc(300);
Quote:
> char buffer[300];
declares an array of 300 characters, fine.
Quote:
> urlstring=NULL;
changes urlstring to point to nothing. Now you have lost the pointer to
the memory allocated by malloc, so you will never be able to free it.
Why allocate memory you never use?
Quote:
> ifstream inFile(filename);
> while (!inFile.eof())
> {
> inFile.getline(buffer,300);
Probably reads stuff into buffer; this looks like c++, which has its own
newsgroup.
Quote:
> urlstring=strstr(buffer,"URL=");
Find the first occurrence of URL= in buffer. Since this sets urlstring,
you really didn't need to set it to NULL earlier.
Quote:
> }
> inFile.close();
> }
> free(urlstring);
Passes something to free that was not allocated by malloc or calloc. A
very bad error.
Quote:
> }
> /////////////////////////////////////////////
> it works well when the program reads only few files...
> but it crash when there are large amount of
> bookmark files...the reason is the program keep allocate new memory block
> for the variable "urlstring" in each call of this function but the memory
> isn't free until the program terminated, so keep callin this function
> will drain up all the memory & crash..
> but my question is...I've placed the statment "free(urlstring)" in the
> end of the function...why the program wont free the memory block in the
> end of this function but in program termination instead?
> pls help...
This appears to be a program that can be fixed just by taking things
out. (I assume there is something more to go in it to actually use the
information it finds.)
void Cpopupdiag::ReadFileContent(CString filename)
{
char *urlstring;
char buffer[300];
ifstream inFile(filename);
while (!inFile.eof())
{
inFile.getline(buffer,300);
urlstring=strstr(buffer,"URL=");
}
inFile.close();
Quote:
}