# c++ new throws exception, no idea why

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i am having a problem with new. I am calling 'new' many times a second and at some point it throws an exception. I got no idea why. I call: char *_type=new char[l+1]; The value of l is 4 or something like that. And the exception is thrown in __sbh_alloc_block(int):
    //  change size of free entry (front and back)
if (sizeNewFree != 0)
{
pEntry->sizeFront = sizeNewFree;
((PENTRYEND)((char *)pEntry + sizeNewFree -
-->                 sizeof(ENTRYEND)))->sizeBack = sizeNewFree;
}

The value of sizeNewFree is 20367508, memory usage looks fine in task manager (so no giant memory leaks) and i 'delete' all i 'new' when i don't need it any more. So i guess it's not free memory that's the problem. I tried catching the exception but when it's thrown once it keeps getting throwed so that didn't fix anything. Two things that might matter: I am using MSVC++ 6.0. I use multiple threads that all use new and delete extensively. Any help would be great! If you need any more info please ask.

##### Share on other sites
What is the thrown exception?

##### Share on other sites
What exception is thrown? And what's the contents of .what()?

Quote:
 I am calling 'new' many times a second and at some point it throws an exception.

How many times? Are we talking 100s or millions?

Quote:
 i 'delete' all i 'new' when i don't need it any more.

Is there some point where you call new many times before deleting, if so, then how many times approximatly.

Quote:
 I am using MSVC++ 6.0.I use multiple threads that all use new and delete extensively.

I don't have access to MSVC++ 6.0 myself, so I can't check this, but are you sure new is thread-safe? If new have any static variables, and no locks, then there is a good chance you are experiencing a race condition.

##### Share on other sites
I don't know what the thrown exception is, any idea how i can find that out using the debugger or using catch(...)?
edit: It's an Access Violation exception:
First-chance exception in server.exe: 0xC0000005: Access Violation.

Quote:
 Original post by CTarWhat exception is thrown? And what's the contents of .what()?

Isn't .what() just for Java? If not, how can i use it when i use catch(...)?

Quote:
 How many times? Are we talking 100s or millions?

Hundreds.

Quote:
 Is there some point where you call new many times before deleting, if so, then how many times approximatly.

No, maybe 3 threads at the same time calling new 3 or 4 times before deleting it again.

Quote:
 I don't have access to MSVC++ 6.0 myself, so I can't check this, but are you sure new is thread-safe?

I assumed it is (many programs these days use multiple threads) but i am starting to doubt it.

##### Share on other sites
The most likely cause is that you've got heap corruption somewhere in your program that causes bad things to happen when the small block handler goes to allocate a new block.

##### Share on other sites
Quote:
 Original post by Tree PenguinIsn't .what() just for Java? If not, how can i use it when i use catch(...)?

No, every class inherited from std::exception should have a .what() member.

Quote:
 I assumed it is (many programs these days use multiple threads) but i am starting to doubt it.

Yes many programs these days use threads, but not many programs at the time of VC6 used threads. That program is older than the first C++ standard (1998, the second was published in 2003 and we now have technical revisions). This could very well be the source of your problem.

##### Share on other sites
Quote:
 Original post by Tree PenguinI don't know what the thrown exception is, any idea how i can find that out using the debugger or using catch(...)?edit: It's an Access Violation exception:First-chance exception in server.exe: 0xC0000005: Access Violation.

It's worth noting that this is not a C++ exception. Access Violations, like Division by Zero, fall under the category of SEH exceptions, and are not part of the C++ standard and thus not portable to rely on being able to handle. As far as I know, one usually has to take special steps (involving compiling a specially patched version of your compiler) to handle these on OS X/Linux. It's far easier to treat them as bugs, since they represent avoidable error conditions, and 99% of the time, indicate you're doing stuff that surely wasn't what you'd intended.

(SuperPig's Introduction to Debugging is a good starting point for this kind of thing)

Quote:
Quote:
 I don't have access to MSVC++ 6.0 myself, so I can't check this, but are you sure new is thread-safe?

I assumed it is (many programs these days use multiple threads) but i am starting to doubt it.

As has been already mentioned, VS6 is positively ancient. In this train of thought, Visual Studio 2005 Express is available for free from Microsoft, and is far less ancient. I highly recommend trying it out (just make sure you follow the instructions carefully, there's post-installer installation steps that need to be followed).

##### Share on other sites
It turns out to be the multiple threads problem.
For now i fixed it by using new only at places where it's really neccessery and i use a 'notbusynewing' flag, looping the threads that want to use new until it's true, which signales the first thread that sees it is the next one in line.

It's one of the ugliest things i've done in a long time but it's an emergency fix and it works.

##### Share on other sites
There are thread safe new instances out there. Also, seriously upgrade to the FREE .NET 2005. VC6 is buggy and non-complaint, it will bite you in the butt, sooner rather then later.

##### Share on other sites
Quote:
 Original post by Anonymous PosterThere are thread safe new instances out there. Also, seriously upgrade to the FREE .NET 2005.

I know, got no time to switch right now.

Quote:
 VC6 is buggy and non-complaint, it will bite you in the butt, sooner rather then later.

I have used it for a few years now and the only real problem i faced is the problem i just discussed.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×