LPTSTR result = new TCHAR;
time_t _currentTime_t = time(0);
_tasctime_s(result, _tcslen(result), &now);
_currentTime = result;
delete result; //error occurs here
There are a few problems with your code that I can see:
_tasctime_sis the number of elements in the buffer provided. In other words,
1024. But you pass
_tcslen(result)which is the length of the null-terminated string. Not only is that the wrong value, but
resultis at that point not initialised, so your code has undefined behaviour.
_currentTime, and then immediately delete that memory. So,
_currentTimeis a stale pointer. Any attempt to read from that memory is yet more undefined behaviour.
I don't want to tell you what your code should be, because you have only given us a tiny window into what you are trying to achieve. Dynamically allocating a fixed length array seems pointless. You may as well use automatically allocated storage. Of course, if you do want to return the memory to the caller, then dynamic allocation makes sense, but in that case then surely the caller would be responsible for calling
delete. Since this code is clearly C++ I have to wonder why you are using raw memory allocation. Why not use standard library classes like
Looking at your update to the question, you could deallocate the memory in the destructor of your class. Personally though, I would recommend learning about the standard library classes that will greatly simplify your code.