EOSERV Forum > Programming > Deleting a class instance?
Topic is locked.
Page: << 1 >>
Deleting a class instance?
Author Message
Post #195656 Deleting a class instance?

I know this question seems stupid to the person who will prlly answer,but I'm not used to accessing class instances this way.

I,m taking myclass(); which I normaly create

MyClass *myclass = new MyClass();

and create it that way I,m trying something different

MyClass myclass = 0;

and creating it like this.

My question is how do I delete it from memory?

10 years, 28 weeks ago
Post #195657 Re: Deleting a class instance?

If you're doing it the second way, it's just going to be deleted at the end of the scope, so you don't need to.

Also, why are you making it equal 0? You can just do "Myclass myclass;" and then set the variables in it to whatever. "myclass.var=0"

10 years, 28 weeks ago
Post #195658 Re: Deleting a class instance?

myclass = 0; was just to shut the compiler up. Alright so i guess its something else within the code I'm not releasing from memory. Thankyou!


Edit: I just got off work and tested my code  and I realized I don't have to create a class instance just use a map and run a loop.

for (std::map<int,MyClass>::iterator i t= myclass.begin(); it != myclass.end(); ++it)
        {

             //MyClass myclass;
             //myclass = it->second;
             // myclass.id = it->first;

I don't think myclass was released from memory because now that im accessing it using it->second.variable the memory releases it fine,before it was leaking about a mb a second!

       
          

10 years, 28 weeks ago
Post #195662 Re: Deleting a class instance?

Allocating using a pointer will compile, but I wouldn't do it that way. You should only use pointer types when absolutely necessary.

Pointer types are initialized as you showed in your example: MyClass *obj = new MyClass(); This will allocate the memory for MyClass dynamically on the heap. Heap-allocated memory needs to be released by using the delete operator like so: delete obj; 

Note that if you're allocating an array of objects, say MyClass *objs = new MyClass[]; you will need to use a different form of the delete operator to de-allocate (ie, free/release) all objects in the array: delete [] objs; The [] indicates that you're releasing an array, not just the first object the array points to.

Objects can also be initialized the way Ryouken said: MyClass obj; will initialize an object of type MyClass on the stack using the default constructor. Stack-allocated memory is (if i recall correctly) managed in the process space and will be freed automatically on program termination, so you won't need to call delete when objects are instantiated this way (in fact, the compiler will throw an error when you try to delete a non-pointer type). This is the way you should probably be doing it.

Accessing members for pointers is done with the arrow operator, that I'm sure you're familiar with: MyClass *obj = new MyClass(); x = obj->Member;

Accessing members for non-pointers uses the dot operator: MyClass obj = MyClass; x =obj.Member;

If you dynamically allocate memory in a class (ie use the new keyword), remember to call delete in your class's destructor.

Looking at the way you do it with the map is completely valid to the compiler. However, that's a terrible way to solve the problem and has so much overhead for something that is built into the language as a feature. If you want, I'd be happy to look over your code to double-check for memory leaks if you want a second set of eyes on your code.

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
10 years, 27 weeks ago
Page: << 1 >>
Topic is locked.
EOSERV Forum > Programming > Deleting a class instance?