Steve
Tue May 31 17:41:06 CDT 2005
Sweet. That was the problem. Thank you so much Emil.
Steve
"Emil Kvarnhammar" <info@ynaxREMOVETHIS.com> wrote in message
news:Og5I6DjZFHA.2756@tk2msftngp13.phx.gbl...
> Hi Steve,
>
> You should not do any cleanup on values returned by ctime.
>
> My suggestion is to remove the line "free(curtime)". The data
> is not dynamically allocated, and should therefore not be free'd
>
> This is probably why the function performs well the first time you
> call it.
>
> kind regards
> Emil Kvarnhammar
>
http://www.ynax.com
>
> "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
> news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
> > Victor,
> > Thanks for your reply. Here's the entire body of the function. I don't
> > seem
> > to be able to get (nothrow) to work. I've include <new>:
> >
> > void WriteMessage(FILE* fp, const char* msg)
> > {
> > time_t curr = time(0);
> > char* curtime = ctime((const time_t*) &curr);
> > char* buffer = new char[(strlen(curtime) + strlen(msg) + 3)];
> > strcpy(buffer, curtime);
> > strcat(buffer, msg);
> > strcat(buffer, "\n\n");
> > if (fp != NULL)
> > fwrite(buffer, 1, strlen(buffer), fp);
> > free(curtime);
> > delete[] buffer;
> > return;
> > }
> >
> >
> > "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> > news:%23%23%23TfRiZFHA.580@TK2MSFTNGP15.phx.gbl...
> >> Steve Long wrote:
> >> > this is probably most fundamental but I don't code in C++ often and
I'm
> > just
> >> > not getting why the code is crashing with a:
> >> > Unhandled exception at 0x77f75a58 in FileIOTest.exe: User breakpoint
> >> >
> >> > I have a function that calls this line of code:
> >> >
> >> > char* buffer = new char[sizeof(char) * (strlen(curtime) + strlen(msg)
+
> > 3)];
> >>
> >> OK, sizeof(char) is always 1, you can easily drop it. Now, what is
> >> 'curtime'? Is it something that can be passed to 'strlen'? What is
> >> 'msg'? Same question, can it be passed to 'strlen'? Remember that
> >> 'strlen' is very fragile, it has undefined behaviour if you pass NULL
> >> to it...
> >>
> >> > The first time I call the function, all is well but subsequent times
it
> >> > crashes with the above exception. All I'm doing is a strcpy, strcat
and
> >> > writing to a file in the funciton and then I delete buffer memory
using
> >> > delete.
> >> >
> >> > What could the problem be. Sorry is this is just really stupid...
> >>
> >> Could be that you're just running out of memory... Try
> >>
> >> char* buffer = new (nothrow) char[strlen(curtime)
> >> + strlen(msg) + 3];
> >>
> >> and see if NULL is returned.
> >>
> >> V
> >
> >
>
>