View previous topic :: View next topic |
Author |
Message |
zile Advanced Cheater Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Wed Jun 22, 2011 5:18 am Post subject: Memory leaks in C++ |
|
|
Its my first time using classes, and its something like this
Code: | class Stream {
private:
unsigned char* data;
int position;
int len;
public:
Stream(unsigned char* packet,int length){
len = length;
if (len > MAX_LENGTH || len <= 0)
return;
position = 0;
this->data = packet;
}
}; |
i wanna ask if c++ has garbage cleaner thing like java? will the new class created destroy itself if it isnt used anymore?
or do i HAVE to make a destructor/ or just delete the class?
mind giving me a proper way to clean this up please?
|
|
Back to top |
|
|
Jesper Grandmaster Cheater Supreme Reputation: 9
Joined: 21 Feb 2007 Posts: 1156
|
Posted: Wed Jun 22, 2011 8:41 am Post subject: |
|
|
I think in C++ you have to do the cleaning all by yourself.
|
|
Back to top |
|
|
atom0s Moderator Reputation: 198
Joined: 25 Jan 2006 Posts: 8516 Location: 127.0.0.1
|
Posted: Wed Jun 22, 2011 12:53 pm Post subject: |
|
|
Look into smart pointers if you want things to start cleaning themselves. Boost has a lot of useful things for this kind of stuff.
_________________
- Retired. |
|
Back to top |
|
|
zile Advanced Cheater Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Thu Jun 23, 2011 8:02 am Post subject: |
|
|
so for the example above,
i tried
Code: |
Stream data = new Stream(...);
delete &data;
|
but i got a heap error, any idea whats wrong?
|
|
Back to top |
|
|
hcavolsdsadgadsg I'm a spammer Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Thu Jun 23, 2011 8:07 am Post subject: |
|
|
Stream* data = new Stream(...);
delete data;
|
|
Back to top |
|
|
zile Advanced Cheater Reputation: 0
Joined: 11 Jul 2009 Posts: 75
|
Posted: Thu Jun 23, 2011 9:01 am Post subject: |
|
|
edit: thanks above, it works!
|
|
Back to top |
|
|
|