causes memory leak...?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 31
Reputation: xyzt is an unknown quantity at this point 
Solved Threads: 0
xyzt xyzt is offline Offline
Light Poster

causes memory leak...?

 
0
  #1
Jul 20th, 2008
hello,
i want to learn if there's memory leak problem in my code...Let me explain:
I have a Customer class,that class also have a destructor function.
I have a static function in that class which returns n Customer objects.
  1. static Customer* createCustomers(int n){
  2. //...
  3. }
  1. int main(int argc, char** argv) {
  2. int n=10;
  3. Customer* customers = Customer::createCustomers(n);
  4. for (int i=0;i<n;i++){
  5. customers[i].display();
  6. }
  7. //delete customers;
  8. }

These n Customer objects which are created by Customer::createCustomers function can be destructed by the destructor function successfully, but when I try to delete the customers pointer, "double freed" error occurs.because the customer objects are being tried to deleted again. if i don't delete the customers this will cause memory leak or not?

here's my whole code if needed..:

  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <iostream>
  4. using namespace std;
  5. #define MAXBUF 80
  6.  
  7. class Customer{
  8. private:
  9. int id;
  10. char *name, *surname, *address;
  11. public:
  12. Customer(){
  13. this->id = 0;
  14. this->name=new char[MAXBUF];
  15. this->surname=new char[MAXBUF];
  16. this->address=new char[MAXBUF];
  17. }
  18. void set_id(int id_){this->id = id_;}
  19. void set_name(char* name_){strcpy(this->name, name_);}
  20. void set_surname(char* surname_){strcpy(this->surname, surname_);}
  21. void set_addres(char* address_){strcpy(this->address, address_);}
  22. int get_id(){return this->id;}
  23. char* get_name(){return this->name;}
  24. char* get_surname(){return this->surname;}
  25. char* get_address(){return this->address;}
  26. void display(){
  27. cout<<"********Customer Info*********\n";
  28. cout<<"ID........:"<<this->get_id()<<endl;
  29. cout<<"Name......:"<<this->get_name()<<endl;
  30. cout<<"Surname...:"<<this->get_surname()<<endl;
  31. cout<<"Address...:"<<this->get_address()<<endl;
  32. }
  33. ~Customer(){
  34. delete name, surname, address;
  35. cout<<"Customer removed from the memory...\n";
  36. }
  37. static Customer* createCustomers(int n){
  38. Customer* customers=new Customer[n];
  39. char* id_str = new char[MAXBUF];
  40. char* namestr = new char[MAXBUF];
  41. char* surnamestr = new char[MAXBUF];
  42. char* addressstr = new char[MAXBUF];
  43. for (int i=0;i<n;i++){
  44. Customer c;
  45. strcpy(namestr, "name");
  46. strcpy(surnamestr, "surname");
  47. strcpy(addressstr, "address");
  48. sprintf(id_str, "%d", i);
  49. strcat(namestr, id_str);
  50. strcat(surnamestr, id_str);
  51. strcat(addressstr, id_str);
  52. c.set_id(i);
  53. c.set_name(namestr);
  54. c.set_surname(surnamestr);
  55. c.set_addres(addressstr);
  56. customers[i]=c;
  57. }
  58. delete namestr, surnamestr, addressstr;
  59. delete id_str;
  60. return customers;
  61. }
  62. };
  63.  
  64. int main(int argc, char** argv) {
  65. int n=10;
  66. Customer* customers = Customer::createCustomers(n);
  67. for (int i=0;i<n;i++){
  68. customers[i].display();
  69. }
  70. //delete customers;
  71. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: causes memory leak...?

 
0
  #2
Jul 20th, 2008
It leaks like a sieve.

> delete namestr, surnamestr, addressstr;
First of all, since you used [ ] in the new, you MUST use [ ] in the delete.
Second, the comma operator doesn't mean "do this to all these things", it means "do it to the last thing".
So all you've got at the moment is
delete addressstr;
whereas what you want is
delete [ ] namestr;
delete [ ] surnamestr;
delete [ ] addressstr;

Plus, the whole static create customers thing just seems wrong to me.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: causes memory leak...?

 
0
  #3
Jul 20th, 2008
Originally Posted by Salem View Post
Plus, the whole static create customers thing just seems wrong to me.
I agree -- it would be better to allocate an array of Customer objects than to have one object allocate an array of data within that instance
  1. int main(int argc, char** argv) {
  2. int n=10;
  3. Customer* customers = new Customer[n];
  4. for (int i=0;i<n;i++){
  5. customers[i].display();
  6. }
  7. delete[] customers;
  8. }

The class should also use std::string instead of char* because (1) its c++ instead of c, and (2) your program doesn't have to worry about memory allocation/deletion.
  1. class Customer{
  2. private:
  3. int id;
  4. std::string name;
  5. std::string surname;
  6. std::string address;
  7. public:
  8. Customer(){
  9. this->id = 0;
  10. }
  11. void set_id(int id_){this->id = id_;}
  12. void set_name(std::string name_){this->name = name;}
  13. void set_surname(std::string surname_){this->surname = surname;}
  14. void set_addres(std::string address_){this->address = address;}
  15. int get_id(){return this->id;}
  16. std::string get_name(){return this->name;}
  17. std::string get_surname(){return this->surname;}
  18. std::string get_address(){return this->address;}
  19. void display(){
  20. cout<<"********Customer Info*********\n";
  21. cout<<"ID........:"<<this->get_id()<<endl;
  22. cout<<"Name......:"<<this->get_name()<<endl;
  23. cout<<"Surname...:"<<this->get_surname()<<endl;
  24. cout<<"Address...:"<<this->get_address()<<endl;
  25. }
  26. ~Customer(){
  27. // nothing to do
  28. }
  29. // delete the static method because its not needed.
  30. };
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: causes memory leak...?

 
0
  #4
Jul 20th, 2008
Looking at that static method some more, I think what you really want is a vector of Customer objects.

  1. int main()
  2. {
  3. vector<Customer> customers;
  4.  
  5. for (int i=0;i<n;i++){
  6. Customer c;
  7. stringstream s;
  8. s << i;
  9. c.set_id(i);
  10. c.set_name("name" + s.str());
  11. c.set_surname("surname" + s.str());
  12. c.set_addres("address" + s.str());
  13. customers.push_back(c);
  14. }
  15.  
  16. ...
  17. ...
  18. vector<Customer>::iterator it;
  19. for(it = customers.begin(); it != customers.end(); it++)
  20. *it->display();
Last edited by Ancient Dragon; Jul 20th, 2008 at 6:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC