473,388 Members | 1,213 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,388 software developers and data experts.

problem delete class object contained thread

classA
{
public:

thirdPartyThreadLibrary myThread;

void Run()
{
myThread.exec(); // created new thread and run on background.
}

};
****LOT OF FUNCTION HERE****

//----------------------------------------------------------------
void myGUIFunctionNamed_Execute()
{
classA * objectA = new classA();
objectA->Run();
delete objectA <---- here is my big problem.If i put this
line, surely myThread in classA
deleted.auto_ptr
also cannot help.If i dont put this line,
my code is perfectly run,but of course
memory leaked.
}
//----------------------------------------------------------------
****LOT OF FUNCTION HERE****
My problem is my object of thread inside classA and object of classA
inside function myGUIFunctionNamed_Execute().I want
myGUIFunctionNamed_Execute()
return immediately after i called it,but i dont no how to delete
object(objectA)
inside this function.My idea is to use auto pointer but when i using
this line ;

auto_ptr<classAobjectA(new classA());

is no luck. :(

Jul 26 '06 #1
5 2891
morz wrote:
classA
{
public:

thirdPartyThreadLibrary myThread;

void Run()
{
myThread.exec(); // created new thread and run on background.
}

};
****LOT OF FUNCTION HERE****

//----------------------------------------------------------------
void myGUIFunctionNamed_Execute()
{
classA * objectA = new classA();
objectA->Run();
delete objectA <---- here is my big problem.If i put this
line, surely myThread in classA
deleted.auto_ptr
also cannot help.If i dont put this line,
my code is perfectly run,but of course
memory leaked.
}
//----------------------------------------------------------------
****LOT OF FUNCTION HERE****
My problem is my object of thread inside classA and object of classA
inside function myGUIFunctionNamed_Execute().I want
myGUIFunctionNamed_Execute()
return immediately after i called it,but i dont no how to delete
object(objectA)
If the object is automatic, rather than dynamic, you won't have this
problem.

You still have an issue with the lifetime of the object and the thread.
It generally isn't a good idea to delete an object that contains a
thread, assuming the thread uses the object's data, while the thread is
running.

--
Ian Collins.
Jul 26 '06 #2
hurm... i want objectA dont destroy until my thread is finish even my
caller function is out of scope.

Jul 26 '06 #3
morz wrote:
hurm... i want objectA dont destroy until my thread is finish even my
caller function is out of scope.
Do what?

Please quote the context you are replying to.

Sounds to me like you want to have a good think about your design and
the lifetime of your objects and threads.

--
Ian Collins.
Jul 26 '06 #4
morz wrote:
hurm... i want objectA dont destroy until my thread is finish even my
caller function is out of scope.
You can make the destructor private, and implement a public release()
method which waits until the thread has finished, and then does delete
this;. Notice: If you do this you always have to use pointers, new and
the method release() to create/destory objects of your class.

Jul 26 '06 #5

fs*********@gmail.com wrote:
morz wrote:
hurm... i want objectA dont destroy until my thread is finish even my
caller function is out of scope.

You can make the destructor private, and implement a public release()
method which waits until the thread has finished, and then does delete
this;. Notice: If you do this you always have to use pointers, new and
the method release() to create/destory objects of your class.

can u give me some example.sound interesting to me but i dont
understand to implement it.

Jul 27 '06 #6

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

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
5
by: Luke Dalessandro | last post by:
Code: Thread -> U -> T public class Thread { protected: thread_t _tid; virtual void foo() = 0; public: // Static entry function for the internal thread
4
by: Flare | last post by:
OK. I'll try explain my problem so simple as possible. I have to send a complex data type to a WebService from a Asp.net webapplication. My Data type look like this. (A class with a porperty)...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
14
by: Christian Kaiser | last post by:
We have a component that has no window. Well, no window in managed code - it uses a DLL which itself uses a window, and this is our problem! When the garbage collector runs and removes our...
5
by: tom | last post by:
Hi, I'm overriding my operator new and operator delete for two classes, one inherited from the other, so I can use my own memory pool. A simplified version of what I have is below: class...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
6
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.