473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

delete causes crash with character arrays

GNU g++ 3.3.3, Cygwin
// Stuff

static char* mbuffer = NULL;

// Stuff

void doit()
{
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);

// Stuff

assert (mbuffer != NULL);
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;

// Stuff

}

How can one detect what causes the crash?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #1
10 3456
Alex Vinokur wrote:
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);
What is nothrow?
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;


It looks like you're deleting memory allocated by placement new with a
"normal" delete. This is illegal IIRC. You will have to define your own
"placement delete" function.

--
Regards,
Matthias
Jul 23 '05 #2

"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur wrote:
// Stuff ------------------------------------------------- mbuffer = new (nothrow) char [1001]
// The program crashes at 'delete[] mbuffer' with the line below (instead of line above) too
mbuffer = new char [1001];
------------------------------------------------- assert (mbuffer != NULL);


What is nothrow?
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;


It looks like you're deleting memory allocated by placement new with a
"normal" delete. This is illegal IIRC. You will have to define your own
"placement delete" function.

--
Regards,
Matthias

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #3

"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur wrote:
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);


What is nothrow?


<QUOTE from http://groups-beta.google.com/group/...565d1c77795e52 >

If you want 'new' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];

</QUOTE>

[snip]
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #4
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:37*************@individual.net...
GNU g++ 3.3.3, Cygwin .... mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);

// Stuff Methinks this Stuff is what actually causes the crash.
assert (mbuffer != NULL);
delete[] mbuffer; // Sometimes crashed here (not always).


Except if you somehow change the value of mbuffer,
some buffer overflow is most likely corrupting the heap
and causing the crash.

Most platforms have tools that can help you detect heap
corruption, or a debugging implementation of heap allocation
functions.
A poor man's first try could also be to do:
mbuffer = 1+new (nothrow) char [1001+2];
mbuffer[-1] = 0x77; //magic number to detect overwrite
mbuffer[1001] = 0x77;

//Stuff... <-- look for change of mbuffer[-1 or 1001]
// e.g. by inserting assertions..

delete[] (mbuffer-1);

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #5
Alex Vinokur wrote:
"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur wrote:
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);


What is nothrow?

<QUOTE from http://groups-beta.google.com/group/...565d1c77795e52 >

If you want 'new' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];

</QUOTE>

[snip]


So your call to new is not a call to placement new, but to the normal
new with the option "don't throw bad_alloc exception" set?

--
Regards,
Matthias
Jul 23 '05 #6

"Ivan Vecerina" <IN*************************@vecerina.com> wrote in message news:cu**********@news.hispeed.ch...
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:37*************@individual.net...
GNU g++ 3.3.3, Cygwin

...
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);

// Stuff

Methinks this Stuff is what actually causes the crash.
assert (mbuffer != NULL);
delete[] mbuffer; // Sometimes crashed here (not always).


Except if you somehow change the value of mbuffer,
some buffer overflow is most likely corrupting the heap
and causing the crash.

Most platforms have tools that can help you detect heap
corruption, or a debugging implementation of heap allocation
functions.
A poor man's first try could also be to do:
mbuffer = 1+new (nothrow) char [1001+2];
mbuffer[-1] = 0x77; //magic number to detect overwrite
mbuffer[1001] = 0x77;

//Stuff... <-- look for change of mbuffer[-1 or 1001]
// e.g. by inserting assertions..

delete[] (mbuffer-1);

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


Ivan,
I started to apply your method and while analyzing the code I found out the cause of the problem.
Of course, it was trampling memory.

infile.read (mbuffer, infile_size); // infile_size was greater than mbuffer size.

Thank you very much.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #7

"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur wrote:
"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur wrote:

// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);

What is nothrow?

<QUOTE from http://groups-beta.google.com/group/...565d1c77795e52 >

If you want 'new' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];

</QUOTE>

[snip]


So your call to new is not a call to placement new, but to the normal
new with the option "don't throw bad_alloc exception" set?


[snip]

Yes. It is not a call to placement new, it is the normal new with the option that returns NULL if failed.

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Jul 23 '05 #8
Matthias wrote:
It looks like you're deleting memory allocated by placement new with a
"normal" delete. This is illegal IIRC. You will have to define your own
"placement delete" function.


Defining a placement delete function won't help (if you mean defining
a deallocation function: operator delete). The placement delete deallocator
is only called if an exception happens during placement construction.

You need to do an explicit destructor call (if necessary for your type) and
then call the whatever deallocation is appropriate.
Jul 23 '05 #9
Ron Natalie wrote:
You need to do an explicit destructor call (if necessary for your type) and
then call the whatever deallocation is appropriate.


Does that mean, placement new doesn't allocate memory in the first
place? And what would be a proper deallocation?

--
Regards,
Matthias
Jul 23 '05 #10
Matthias wrote:

Ron Natalie wrote:
You need to do an explicit destructor call (if necessary for your type) and
then call the whatever deallocation is appropriate.
Does that mean, placement new doesn't allocate memory in the first
place?


Exactly that
And what would be a proper deallocation?


Whatever is the opposite of the allocation strategy you used
for providing the memory.

The whole point of placement new is that 'new' no longer cares
to reserve some memory, but it is the job of the programmer
to allocate (or don't allocate at all) it. 'placement new' simply
takes the raw memory you give to it and turn it into an object
(by calling a constructor). Everything else is the responsibility
of the programmer.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
15
by: greenflame | last post by:
First of all I only have IE for testing. Ok. I have a script that is supposed to show, not evaluate, the indefinite integral of something. When I run the script it just craches IE. I have tried...
8
by: Adam Louis | last post by:
I would like help resolving this problem. I'm a novice who's been hired to query a hospital database and extract useful information, available to me only in a dynamically generated, downloadable...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
2
by: rich | last post by:
I am building a database and I am using a list where I can make multiple choices. The data is like this Master table item1id item2 index(item1id) detail table item1id
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.