Question

Memory allocation in different threads (B C++ Builder)

Asked by: RobertB

Hello everybody,

I have separated the following code from a large project. It crushes when I
click a form one or two times:

Simple thread with the following Execute method:

void __fastcall SGModule::Execute() {
short *fdata;
 while(!Terminated) {
    fdata = new short[10000];
    delete[] fdata;
  }
}

Event handler for TForm click:
void __fastcall TForm1::FormClick(TObject *Sender)
{
  char *x;
  for(unsigned i=0;i<1000;i++) {
    x = new char[10];
    delete[] x;
  }
}

I am imcluding the only header file
#include <vcl.h>

I have tried to set IsMultiThread variable to true, which should solve the problem, but it haven't.
Does anybody know anything more about memory allocation in a multi threaded application in C++ Builder?

Any ideas welcome

Robert Batusek

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2000-09-01 at 01:53:03ID11167632
Topic

C++ Programming Language

Participating Experts
3
Points
250
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. LoadFromFile in C++ Builder
    In my program i try to load several Bitmaps while the program is running. In Delphi 2.0 exists the method LoadFromFile. Using this method the following code works Image1.Picture.LoadFromFile('C:\pictures\example.bmp');. The analogous code using the -> operator instead of ...
  2. Borland C++Builder events
    I want to make a DLL using C++Builder 3 and a VCL SMTP object. How do I handle events coming from the object? The object is created at run-time and I dont have a form. A sample code would be great.
  3. Borland Builder and TrayIcon
    How can i use TrayIcons in my Borland Builder App ? Is there an example code or Builder Component ?
  4. MAking dll's with Borland C++ Builder 5
    Hi everybody I’ve got a rather basic problem. I would like to create a dll with Borland C++ builder 5. I have followed all the examples in the help menu and did some trial and error work myself. Unfortunately, the thing still doesn’t run. I can compile the dll as well as the...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: nietodPosted on 2000-09-01 at 03:51:36ID: 4154767

You must use the multi-threaded libraries (specified in the compiler settings) and then new and delete will be multi-thread safe.  Are you?

Are you sure this is where it is crashing?  there must be other code too.

 

by: RobertBPosted on 2000-09-01 at 04:16:17ID: 4154981

Of course I am sure. I am really not a beginner and I have spent about 4 days by debugging (i. e. separating this code from thousands of lines of code). When I place new and delete statements into critical section, it stops to crash.
I can send you the whole code, if you are interested.

But maybe you are right with the run-time libraries. Where I have to set it up? I have tried to browse all option settings But I haven't been successfull.

Thanks.

Robert

 

by: nietodPosted on 2000-09-01 at 04:30:05ID: 4155141

>> it stops to crash.
You mean it stops the crashing?  or  it stops and crashes?

How are you starting your threads?   You must use the C++ RTL procedures like _beginthread() or _beginthreadEx().  You cannot use CreateThread() safely.

 

by: RobertBPosted on 2000-09-01 at 04:54:19ID: 4155330

Of course I am sure. I am really not a beginner and I have spent about 4 days by debugging (i. e. separating this code from thousands of lines of code). When I place new and delete statements into critical section, it stops to crash.
I can send you the whole code, if you are interested.

But maybe you are right with the run-time libraries. Where I have to set it up? I have tried to browse all option settings But I haven't been successfull.

Thanks.

Robert

 

by: RobertBPosted on 2000-09-01 at 04:57:24ID: 4155341

It stops crashing. It doesn't crash anymore.

I am using TThread class, or exactly its descendant:

SGModule sgm = new SGModule();

with constructor defined as:
SGModule::SGModule() : TThread(false){}


Sorry for my English

Robert

 

by: nietodPosted on 2000-09-01 at 05:13:17ID: 4155494

>> Sorry for my English
I didn't even know that English wasn't your first language!

Did you change something?  

If not the problem could come back...

Are there multiple files in your project?  Did you recompile/link some that had not been compiled/linked since you switched to multi-threading.

 

by: RobertBPosted on 2000-09-01 at 05:24:20ID: 4155641

I still didn't fix the problem.
I'll summarize it:

1. The code above crushes after some time.

2. When a synchronization technique is used to allocation/deallocation, it works OK.

IMHO it means that the memory manager is not thread-safe. And my question is:
How to make it thread safe? Is it possible?
How do I ensure I am using the right (multi-threaded) run-time libraries? If not, how can I switch to the right libraries?
And what about IsMultiThread global variable? Is it worth to change it?


Robert

 

by: nietodPosted on 2000-09-01 at 05:35:51ID: 4155733

>> IMHO it means that the memory manager
>> is not thread-safe
It appears that in Builder the memory manager is thread safe if the IsMultiThread global variable is set to true.  Furthermore this is set to true automatically when a thread is started through the library procedures.  So there should not be a problem.   But you should check to make sure it is set to true.  If it is set to false, that woudl be a problem.

Borland does not seem to have seperate single and multi-threaded libraries.  VC does, and you woudl get this problem if you used the single-threaded library, but not with the multi-threaded library.

 

by: AlexVirochovskyPosted on 2000-09-01 at 06:03:32ID: 4156121

It is well known problem in BC 5.x, but it is suprice for me , that same problem exists in BCB.
Problem: after some times (~ 25,depends from apps) new /delete crashed in MultiThread project(if exist > 1 thread, of couse).
Solution 1: overload new/delete to you
own(use , for example, alloc/faralloc)
Solution 2: sincronisation (Critical section,Mutex,...)
In general: new/delete are NOT threadsaved in BC 5.x and now I see same in BCB.

By my info (before ~1 year): problem
exists in BC 5.02 too(but may be solved
in some patch).
Alex

 

by: nietodPosted on 2000-09-01 at 06:51:24ID: 4156722

Are you sure that it isn't safe?  That would be a inexcusable error on Borland's part.

 

by: LucHoltkampPosted on 2000-09-01 at 06:55:29ID: 4156762

I had the same problem in BC5, I spend days looking for it, (it never appeared on the same place) and even issued a bug report to borland (on which i never got a reply).
But it is true... borlands BC5 heap is not serialised and therefore not threadsafe.
The problem also was that the DLL's that BC5 programs use already had the heap linked to it, so overloading new and delete was no good, the dll's then use a different new then you're own code and it will crash. (Tried that... it crashed). This happens if you create a variable from the heap that is destroyed in the library or vice versa, something that will happen if you use Windows (OWL in BC5) objects. This is logically of cource, because only calls to new and delete that are linked are going to your overloaded new, the one's in the dll's don't (they were linked earlier, and know nothing of your overloaded new and delete).
Overloading new and delete globally can be considered bad practice!!
Finally I decided to STATICALLY link the app, and replace malloc and free in the library with winapi calls (LocalAlloc etc), to do that, you must localize the module in the library that contains the heap, there are two of them, (use TDump) write your own malloc and free and add them to the library. Unfortunatly there are some extra functions in those modules for heap checking (heap_walk etc), if you don't use them you can replace them with simple stubs (empty functions).
Things become simpler if you only use your own classes multithreaded.. in that case you can overload new and delete for that class.
Finally I must say that I was astonished when I found this bug... how could they ever be so stupid? And I'm even more astonished that they didn't fix it in BCB. Alex: I tried every patch in BC5 ... no result, and as I said, I issued a bug report, but never got a reply.  Not that that is a suprise there was also a bug in BC5's auto_ptr implementation in STL, unfortunatly I never got a reply on that report too.
Luc

 

by: nietodPosted on 2000-09-01 at 07:09:23ID: 4156944

wow....

 

by: RobertBPosted on 2000-09-04 at 02:40:04ID: 4179250

Now I have proved that the error is on the Borland side and I have to deal with it some how. LucHoltkamp, I will accept your answer, but I have one more question:

Ii is impossible to recompile RTL, because at least in my version of BCB, sources are not included. So, I propose this solution:

I have implemented my own new operator and in all my classes I am using it. I will try to implement also my own Allocator class, as I am using some objects from Standard C++ Library. I am quite sure, that I am not using any other objects or routines, or at least I don't use them in multi-threaded part of my application. Do you think, that this solution is satisfactory?

(In fact, I am testing it now, and it stil doesn't work, although it crushes much less frequently. But there still may be some unnoticed source code.)

And another note: I have reported the bug to Borland, hope that they will at least read it.

Thanks all for remarks

Robert

 

by: LucHoltkampPosted on 2000-09-04 at 05:25:41ID: 4180533

One problem is that in your code, I see you create a char with new, you cannot overload the operator new for char's, and a globally operator new will not work... so your solution only works if you use ONLY your own classes that have a overloaded operator new.

For my solution, you do not need the source code, you just replace the heap module in the library, all the function declaration are known, they are in the headers, so it is possible to rewrite them.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...