| | |
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:
Solved Threads: 0
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?
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
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)
method() { TEnemyCharacter *tenemyCharacter = new TEnemyCharacter(this); }
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
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.
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
#3 Oct 27th, 2009
•
•
•
•
So at what point I have to delete this pointer?
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
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#4 Oct 27th, 2009
•
•
•
•
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.
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)
class Thread { public: Thread(IRunnable *ptr=0) { _runnable = ptr; _started = false; _threadHandle = 0; } ~Thread() { if(_threadHandle != 0) ::CloseHandle(_threadHandle); } void start(IRunnable *ptr=0) { if(_started) throw ThreadException("Thread already started."); if(!_started && _threadHandle != 0) ::CloseHandle(_threadHandle); if(ptr != 0) _runnable = ptr; if(_runnable == 0) throw ThreadException("An object implementing the IRunnable interface required."); DWORD threadID=0; _threadHandle = ::CreateThread(0, 0, ThreadProc, this, 0, &threadID); if(_threadHandle == 0) throw ThreadException(::GetLastError()); ::Sleep(0); } void stop() { checkAlive(); _runnable->stop(); } void suspend() { checkAlive(); checkThreadHandle(); if(::SuspendThread(_threadHandle) == -1) throw ThreadException(::GetLastError()); } void resume() { checkAlive(); checkThreadHandle(); if(::ResumeThread(_threadHandle) == -1) throw ThreadException(::GetLastError()); } void join(unsigned long timeOut=INFINITE) { checkThreadHandle(); if(isAlive()) { DWORD waitResult = ::WaitForSingleObject(_threadHandle, timeOut); if(waitResult == WAIT_FAILED) throw ThreadException(::GetLastError()); } } bool isAlive() { return _started; } protected: bool _started; HANDLE _threadHandle; IRunnable *_runnable; unsigned long run() { _started = true; unsigned long threadExitCode = _runnable->run(); _started = false; return threadExitCode; } void checkThreadHandle() { if(_threadHandle == 0) throw ThreadException("Thread not yet created, call the start() method."); } void checkAlive() { if(!isAlive()) throw ThreadException("No Thread alive."); } static unsigned long __stdcall ThreadProc(void* ptr) { return ((Thread *)ptr)->run(); } };
Should I put delete in the constructor of that class
~Thread() {
if(_threadHandle != 0)
::CloseHandle(_threadHandle);
delete _runnable; // addition
}
![]() |
Similar Threads
- How delete the pointer in Composite Objects? (C++)
- Passing pointer to matrix to caller? (C++)
- Error when calling a method of dynamic array (C++)
- Pointer Namespace Method Help (C++)
- SQLDataSource Delete() (ASP.NET)
- delete a row in a sql databse (ASP.NET)
- Help: Passing arrays between functions (C)
Other Threads in the C++ Forum
- Previous Thread: Inserting Item In Template Array
- Next Thread: How to override standard printf defined in gcc library
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





