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?

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

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.

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.

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

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
}

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.

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.
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.