Board index » cppbuilder » A strange execution problem (2)

A strange execution problem (2)


2004-07-20 06:20:10 PM
cppbuilder33
Hi again,
First of all, many thanks to all for your quick answers.
About my first problem exposed, I've just removed de string
deletion, as you all have suggested, and obviusly the servers
don't crash.
Regarding, the second one, I've tried the most popular anwer
(deleting the thread before NULLing the pointer):
Quote
// Now the threads are finished
for (int i = 0; (i < N_servers); i++)
{
delete th[i];
Quote
th[i] = NULL;
}

delete[] th;
th = NULL;
and the client crashes once and again it executes the new
code line. So, it is no a good idea. Perhaps it's because
I've created the threads with the "FreeOnTerminate"
property equal to true (that's the reason why I only NULL
every pointer in the array of pointers to server-manager
threads, to destroy the array).
By the way, the Exception issued is "Access violation"
to address 0x00000000. This might be because the pointer
th[i] is already NULL, isn't it? So I could simply skip the
for loop, and directly "delete[]" the array, that's right?
Just one thing. I want to keep the reference to every thread
in order to manage an eventual termination order using
th[i]->Terminate();
Any other ideas to solve the problem, please?
Moreover, under certain running conditions, the above
problem is skipped, but the client application crashes when
creating (dynamically) an instance of an object that is used
masively in my application, and that has been checked to
work fine (freeing the memory used and so on).
Thanks again,
Juan-Carlos Baraza.
 
 

Re:A strange execution problem (2)

On Tue, 20 Jul 2004 12:20:10 +0200, J. Carlos Baraza wrote:
Quote
First of all, many thanks to all for your quick answers.
First of all, please don't cross post or multipost. Pick a single group
for your postings. I've cross posted this, but set follow-up to one
place.
Quote
client application crashes when
creating (dynamically) an instance of an object that is used
masively in my application, and that has been checked to
work fine (freeing the memory used and so on).
Have you considered smart pointers? auto_ptr comes with the compiler,
but there's also e.g. boost::scoped_ptr and boost::shared_ptr
www.boost.org/libs/smart_ptr/smart_ptr.htm
--
Good luck,
liz
 

Re:A strange execution problem (2)

Please direct your browser at info.borland.com/newsgroups/ and read the
newsgroup guidelines. One of them asks us not to post the same question to
different newsgroups, but to pick the most appropriate one and just post
there. Thanks!
Followup-To: borland.public.cppbuilder.language.cpp; let's keep the discussion
in that group.
"J. Carlos Baraza" < XXXX@XXXXX.COM >writes:
Quote
Regarding, the second one, I've tried the most popular anwer
(deleting the thread before NULLing the pointer):
Popularity may vary, but is irrelevant. The solution that is both the
simplest and the easiest to get correct is to use a container of smart
pointers.
Quote

>// Now the threads are finished
>for (int i = 0; (i < N_servers); i++)
{
delete th[i];

>th[i] = NULL;
There is no point in this assignment since the next thing you are doing
with th is delete[]ing it.
Quote
}
>
>delete[] th;
>th = NULL;

and the client crashes once and again it executes the new
code line.
Which one is that?
Quote
So, it is no a good idea. Perhaps it's because
I've created the threads with the "FreeOnTerminate"
property equal to true (that's the reason why I only NULL
every pointer in the array of pointers to server-manager
threads, to destroy the array).
That's certainly the reason. In that case, make th a
std::vector<YourThreadClass>
(you have started a new thread so I don't know the real name of your thread
class).
Quote
By the way, the Exception issued is "Access violation"
to address 0x00000000. This might be because the pointer
th[i] is already NULL, isn't it?
Very unlikely.
deleteing a null pointer is a perfectly valid operation, doing nothing at
all. The access violation is a symptom of attempting to destruct the same
object twice.
Quote
So I could simply skip the
for loop, and directly "delete[]" the array, that's right?
Yes.
Even more simply, you could make th a continer object anod skip the delete []
expression as well. (Yes, I am aware that I know I am repeating myself :-) )
Quote
Just one thing. I want to keep the reference to every thread
in order to manage an eventual termination order using
th[i]->Terminate();
This will not be straightforward; chances are that the thread represented
by the object *th[i] has already terminated. Since you set FreeOnTerminate,
that object might already be destructed (or its destruction might currently
be happening in the thread to be terminated).
I'd not set FreeOnTerminate and make th a container of smart pointers
(repeating myself again).
That said, you are certainly not the first to have this problem, and other
solutions are likely to exist.
Quote
Moreover, under certain running conditions, the above
problem is skipped, but the client application crashes when
creating (dynamically) an instance of an object that is used
masively in my application, and that has been checked to
work fine (freeing the memory used and so on).
You'd have to post a minimal program demonstrating this problem.
 

{smallsort}

Re:A strange execution problem (2)

"J. Carlos Baraza" < XXXX@XXXXX.COM >wrote in message
Quote
Perhaps it's because I've created the threads with
the "FreeOnTerminate" property equal to true
Never delete a thread that has the FreeOnTerminate property set to true.
You will cause a double deletion of the thread memory, resulting in access
violations.
On the other hand, if you are going to delete a thread object manually then
don't set its FreeOnTerminate property to true.
Quote
By the way, the Exception issued is "Access violation"
to address 0x00000000.
You are accessing a NULL pointer.
Quote
Just one thing. I want to keep the reference to every
thread in order to manage an eventual termination
order using
th[i]->Terminate();
Then you should not be using FreeOnTerminate at all. Otherwise you could
have threads disappear from memory but you would still be holding on to
references to them.
Gambit
P.S. please do not cross-post. It is against Borland's newsgroup guidelines
(info.borland.com/newsgroups/guide.html). Only post to the single
most appropriate group, which in this case is actually the "nativeapi"
group.