944,219 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 891
  • C++ RSS
Oct 27th, 2009
0

delete a pointer inside of a method

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmcdeshan is offline Offline
4 posts
since Jul 2009
Oct 27th, 2009
0
Re: delete a pointer inside of a method
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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 27th, 2009
1
Re: delete a pointer inside of a method
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 27th, 2009
0
Re: delete a pointer inside of a method
Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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

C++ Syntax (Toggle Plain Text)
  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
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmcdeshan is offline Offline
4 posts
since Jul 2009
Oct 27th, 2009
1
Re: delete a pointer inside of a method
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Oct 27th, 2009
0
Re: delete a pointer inside of a method
Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbmcdeshan is offline Offline
4 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inserting Item In Template Array
Next Thread in C++ Forum Timeline: How to override standard printf defined in gcc library





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC