delete a pointer inside of a method

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 4
Reputation: sbmcdeshan is an unknown quantity at this point 
Solved Threads: 0
sbmcdeshan sbmcdeshan is offline Offline
Newbie Poster

delete a pointer inside of a method

 
0
  #1
Oct 27th, 2009
Hi
I have a pointer generated in a method.
And this pointer is input to another object.
So at what point I have to delete this pointer?

  1. method()
  2. {
  3.  
  4. TEnemyCharacter *tenemyCharacter = new TEnemyCharacter(this);
  5.  
  6. }

So how to delete this tenemyCharacter.
I cannot use delete tenemyCharacter; at the method end.
And I cannot declare this in header

Thanx in advance
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #2
Oct 27th, 2009
I wonder if it is compulsary to use new to allocate variable space. Since it is only one. Why dont you just declare a variable. That way you will not need to worry about deleting. as the Destructor will be called at the end of method.

Apart from that, unless method returns the address of the new object . or returns a pointer to that particular object. I think its not possible to delete it.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #3
Oct 27th, 2009
So at what point I have to delete this pointer?
As long as you have a copy of the pointer that was returned by new , you can delete it. You said that the pointer is stored in another object, so that object's destructor would probably handle the deletion. Though that is not exactly the best design, IMO.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: sbmcdeshan is an unknown quantity at this point 
Solved Threads: 0
sbmcdeshan sbmcdeshan is offline Offline
Newbie Poster
 
0
  #4
Oct 27th, 2009
Originally Posted by Tom Gunn View Post
You said that the pointer is stored in another object, so that object's destructor would probably handle the deletion. Though that is not exactly the best design, IMO.
Thank You, I think it's the best solution. There are several points which I cannot understand. Following is the code

EnemyCharacter::EnemyCharacter(...)
{
TEnemyCharacter *tenemyCharacter = new TEnemyCharacter(this);
thread = new Thread(tenemyCharacter);
}

The Thread is a 3rd party wrapper class and not managed to find any deleting of this pointer

  1. class Thread {
  2. public:
  3. Thread(IRunnable *ptr=0) {
  4. _runnable = ptr;
  5. _started = false;
  6. _threadHandle = 0;
  7. }
  8.  
  9. ~Thread() {
  10. if(_threadHandle != 0)
  11. ::CloseHandle(_threadHandle);
  12. }
  13.  
  14. void start(IRunnable *ptr=0) {
  15. if(_started)
  16. throw ThreadException("Thread already started.");
  17.  
  18. if(!_started && _threadHandle != 0)
  19. ::CloseHandle(_threadHandle);
  20.  
  21. if(ptr != 0)
  22. _runnable = ptr;
  23.  
  24. if(_runnable == 0)
  25. throw ThreadException("An object implementing the IRunnable interface required.");
  26.  
  27.  
  28. DWORD threadID=0;
  29. _threadHandle = ::CreateThread(0, 0, ThreadProc, this, 0, &threadID);
  30. if(_threadHandle == 0)
  31. throw ThreadException(::GetLastError());
  32.  
  33. ::Sleep(0);
  34. }
  35.  
  36. void stop() {
  37. checkAlive();
  38. _runnable->stop();
  39. }
  40.  
  41. void suspend() {
  42. checkAlive();
  43. checkThreadHandle();
  44. if(::SuspendThread(_threadHandle) == -1)
  45. throw ThreadException(::GetLastError());
  46. }
  47.  
  48. void resume() {
  49. checkAlive();
  50. checkThreadHandle();
  51. if(::ResumeThread(_threadHandle) == -1)
  52. throw ThreadException(::GetLastError());
  53. }
  54.  
  55. void join(unsigned long timeOut=INFINITE) {
  56. checkThreadHandle();
  57. if(isAlive()) {
  58. DWORD waitResult = ::WaitForSingleObject(_threadHandle, timeOut);
  59. if(waitResult == WAIT_FAILED)
  60. throw ThreadException(::GetLastError());
  61. }
  62. }
  63.  
  64. bool isAlive() { return _started; }
  65.  
  66. protected:
  67.  
  68. bool _started;
  69. HANDLE _threadHandle;
  70. IRunnable *_runnable;
  71.  
  72. unsigned long run() {
  73. _started = true;
  74. unsigned long threadExitCode = _runnable->run();
  75. _started = false;
  76. return threadExitCode;
  77. }
  78.  
  79. void checkThreadHandle() {
  80. if(_threadHandle == 0)
  81. throw ThreadException("Thread not yet created, call the start() method.");
  82. }
  83. void checkAlive() {
  84. if(!isAlive())
  85. throw ThreadException("No Thread alive.");
  86. }
  87.  
  88. static unsigned long __stdcall ThreadProc(void* ptr) {
  89. return ((Thread *)ptr)->run();
  90. }
  91. };

Should I put delete in the constructor of that class

~Thread() {
if(_threadHandle != 0)
::CloseHandle(_threadHandle);
delete _runnable; // addition
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
1
  #5
Oct 27th, 2009
Should I put delete in the constructor of that class
The destructor, yes. Or better yet, use a smart pointer like auto_ptr where you do not need to worry about deleting it at all.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: sbmcdeshan is an unknown quantity at this point 
Solved Threads: 0
sbmcdeshan sbmcdeshan is offline Offline
Newbie Poster
 
0
  #6
Oct 27th, 2009
Originally Posted by Tom Gunn View Post
The destructor, yes. Or better yet, use a smart pointer like auto_ptr where you do not need to worry about deleting it at all.
ya ya it's destructor. And thank You very much for giving me the answer and this new thing call smart pointer.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC