473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

delete object "couse" return crash

Hi, my problem is:
I'm initializing object of class USER (whel pointer actually), adn then deleting it, and everything goes quite OK in the main, until I reach return 0; the windows is trigering a break point, my question, is why... and how to resolve this problem

Some code:

Expand|Select|Wrap|Line Numbers
  1. class USER
  2. {
  3. public:
  4.     char* nickname;
  5.     struct tm * first_contact;
  6.     bool oper;
  7.     bool admin;
  8.  
  9.     USER(char* nickname, bool operat, bool admin);
  10.     ~USER(void);
  11. };
  12.  
  13. USER::USER(char* nickname, bool operat, bool admin)
  14. {
  15.     this->admin = admin;
  16.     this->oper = operat;
  17.     this->nickname = nickname;
  18.  
  19.     time_t czas;
  20.     ::time( & czas );
  21.     this->first_contact = localtime( & czas );
  22. }
  23.  
  24.  
  25. USER::~USER(void)
  26. {
  27.     delete this->first_contact;
  28.     delete this->nickname;
  29. }
  30.  
  31. int main (void){
  32.     char *a = new char[4];
  33.     a[0] = 's';
  34.     a[0] = 'a';
  35.     a[0] = 'd';
  36.     a[0] = 0;
  37.  
  38.     USER *s = new USER(a, 0, 0);
  39.     delete s;
  40.  
  41.     system("pause");
  42.     return 0;
  43. }
using MS visual 2010
thanks for any help
Sep 17 '13 #1

✓ answered by weaknessforcats

You tell me.

If localtime allocates on the heap, and you fail to free the returned pointer, there is a memory leak. Bad.
If localtime does not allocate on the heap and you delete the pointer. Bad.
If localtime does not allocate on the heap and you do not delete the pointer. Good.

Therefore, you have to use the returned pointer to access the members of the returned struct and to copy those members to your own struct tm. The object pointed at by the returned pointer is only valid until the next C library time function call.

An object referred to by a returned pointer is deleted when the function completes or you have a memory leak. That would give you no time to recover the data in the struct tm the returned pointer points at. The only way around this is for the function to use a static variable that sticks around until the next call to a C library time function.

So if you know there will never be another call in your code, or in any other code that includes your code, at any time in the future, then you can just store the pointer. This is not not a wise choice, this business of predicting the future, so I do recommend your own struct tm in your class to keep the result of your call to localtime.

4 1792
ok figured something out, when i comment
//delete this->first_contact;
it runs smothly, i get the dependence in new/delete number/usage, but what is happening whitch first_contact then, is it deleted, or is it still egzisting in memory? (after delete s)
Sep 17 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
first_contact was never allocated using a new operator. That means the compiler is responsible for deleting it and not you.

You can only delete what you allocate using the new operator.
Sep 17 '13 #3
ok, but doesn't the function first_contact = localtime( & czas );
allocate this memory? or is it doing it some weard other way?
Sep 17 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
You tell me.

If localtime allocates on the heap, and you fail to free the returned pointer, there is a memory leak. Bad.
If localtime does not allocate on the heap and you delete the pointer. Bad.
If localtime does not allocate on the heap and you do not delete the pointer. Good.

Therefore, you have to use the returned pointer to access the members of the returned struct and to copy those members to your own struct tm. The object pointed at by the returned pointer is only valid until the next C library time function call.

An object referred to by a returned pointer is deleted when the function completes or you have a memory leak. That would give you no time to recover the data in the struct tm the returned pointer points at. The only way around this is for the function to use a static variable that sticks around until the next call to a C library time function.

So if you know there will never be another call in your code, or in any other code that includes your code, at any time in the future, then you can just store the pointer. This is not not a wise choice, this business of predicting the future, so I do recommend your own struct tm in your class to keep the result of your call to localtime.
Sep 17 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes....
6
by: Sowen | last post by:
hi, I have the following code object obj_1; object obj_2; object obj_3; object *objs = { &obj_1, &obj_2, &obj_3 };
12
by: Yin99 | last post by:
Why am I still able to use this object even though I have deleted it? The Output I get is: Cat Constructs Cat De-structs //deleteing object Cat Eats. //My question- why does object still...
8
by: NicK chlam via DotNetMonster.com | last post by:
is thier any way to create a new object in a function and return that object being able to use it in the main class? like to have a static method that inputs first and last names, creates a...
4
by: surindersaini | last post by:
Hi I am not sure if we can do it and if can, then how. I have a class with a property that return object as its return type. when i am trying to Serialize the class it gives me error ...
0
by: Diego | last post by:
I have a Generic list of my objects set as datasource of a DataGridView. In the cell doubleclick event i want to delete the selected object deleting it from the datasource (the generic list) this...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
1
by: Ryura | last post by:
Hello, I'm currently getting some JSON data (y.x) and then doing something like this: $("dd.tajauser").each(function() { // does the function for each dd.tajauser var z=y.x.length; z-=1; do {...
2
by: thelamb | last post by:
Hello all, I have a question about deleting objects(the objects are stored in a vector) I Fill the vector as following: servObj.push_back(new CGame(serversh, game_id)); (1) ...
0
by: Crystal Thompso | last post by:
I have a action failed message in a Macro when I delete a table that is already deleted. The only option is to Stop All Macros. How do I bypass this error in the macro and let it go on to the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.