Hello,
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)];

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...
Steve

Re: memory allocation problems by Victor

Victor
Tue May 31 16:04:43 CDT 2005

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

Re: memory allocation problems by David

David
Tue May 31 16:58:51 CDT 2005


"Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
news:O5gJsMiZFHA.2420@TK2MSFTNGP12.phx.gbl...

> Hello,
> 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)];
>
> 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...

As Victor says it all rather depends on what te values of curtime
and msg are.

Why not replace it with

int nLenCurTime = strlen(curtime);
int nLenMsg = strlen(msg);
int nBytesNeeded = nLenCurTime+nLenMsg+3;

char *buffer = new char [nBytesNeeded];
.
.
.
delete [] buffer;

and then step through with te debugger.

(I take it you *are* remembering the [] in the delete statement?)

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm







Re: memory allocation problems by Steve

Steve
Tue May 31 17:17:39 CDT 2005

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



Re: memory allocation problems by Simon

Simon
Tue May 31 17:33:01 CDT 2005

"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>:

You may need new (std::nothrow) if you haven't got 'using namespace std'
anywhere in scope (and pray God you have not).

S.



Re: memory allocation problems by Simon

Simon
Tue May 31 17:34:52 CDT 2005

"David Webber" <dave@musical.demon.co.uk> wrote in message
news:ec1vzviZFHA.584@TK2MSFTNGP15.phx.gbl...
> (I take it you *are* remembering the [] in the delete statement?)

On VC6 and 7.0 implementations (haven't tried it on 7.1 or 8b2), it
magically works anyway. It's undefined behavior, of course, but it's
unlikely to cause this particular bug.



Re: memory allocation problems by Emil

Emil
Tue May 31 17:31:51 CDT 2005

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
>
>



Re: memory allocation problems by Steve

Steve
Tue May 31 17:34:27 CDT 2005

I'm passing 25 + 25 + 3. How could that cause an exception? I posted the
code for the entire function body a little earlier.


"David Webber" <dave@musical.demon.co.uk> wrote in message
news:ec1vzviZFHA.584@TK2MSFTNGP15.phx.gbl...
>
> "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
> news:O5gJsMiZFHA.2420@TK2MSFTNGP12.phx.gbl...
>
> > Hello,
> > 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)];
> >
> > 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...
>
> As Victor says it all rather depends on what te values of curtime
> and msg are.
>
> Why not replace it with
>
> int nLenCurTime = strlen(curtime);
> int nLenMsg = strlen(msg);
> int nBytesNeeded = nLenCurTime+nLenMsg+3;
>
> char *buffer = new char [nBytesNeeded];
> .
> .
> .
> delete [] buffer;
>
> and then step through with te debugger.
>
> (I take it you *are* remembering the [] in the delete statement?)
>
> Dave
> --
> David Webber
> Author MOZART the music processor for Windows -
> http://www.mozart.co.uk
> For discussion/support see
> http://www.mozart.co.uk/mzusers/mailinglist.htm
>
>
>
>
>
>



Re: memory allocation problems by Steve

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
> >
> >
>
>



Re: memory allocation problems by David

David
Wed Jun 01 00:17:39 CDT 2005


"Simon Trew" <ten.enagro@werts> wrote in message
news:OgcRzDjZFHA.2796@TK2MSFTNGP10.phx.gbl...

>> (I take it you *are* remembering the [] in the delete statement?)
>
> On VC6 and 7.0 implementations (haven't tried it on 7.1 or 8b2),
> it magically works anyway. It's undefined behavior, of course, but
> it's unlikely to cause this particular bug.

Interesting. I didn't know that.

I'll now try to forget I do and go on writing correct code :-)

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm



Re: memory allocation problems by Alexander

Alexander
Wed Jun 01 00:40:03 CDT 2005

I can't help but wonder what was wrong with simply:

fprintf(fp, "%s%s\n\n", curtime, msg);

No funky allocations, no nothing...

"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
>
>



Re: memory allocation problems by Steve

Steve
Wed Jun 01 11:21:19 CDT 2005

Hmmm, hadn't thought of it. Looks clean. I think I'll try it.

Thanks
Steve

"Alexander Grigoriev" <alegr@earthlink.net> wrote in message
news:%23jFxdxmZFHA.3840@tk2msftngp13.phx.gbl...
> I can't help but wonder what was wrong with simply:
>
> fprintf(fp, "%s%s\n\n", curtime, msg);
>
> No funky allocations, no nothing...
>
> "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
> >
> >
>
>