Can memory leaks only caused by using the new keyword?

Started by
19 comments, last by Infinisearch 8 years ago

Memory can also be directly allocated with malloc() or related functions such as realloc() or calloc() or with direct OS calls such as the HeapAlloc() function in Windows.

Alright, then I'm not sure why my code is leaking.

SHouldn't, have checked my code around 20 times now, and I have deallocated all pointers that I use. :/

Are you sure it's your code leaking and not some external library that you're using?

Did you find all the places in your code that you call malloc or one of the other allocation functions? If you never call new then that has to be the cause.

Don't know actually.

How would I go about finding the leak if thats the case?

Would I have to check every single function that I use in my code and see what I need to do on the documentation of my library?

Advertisement


How have you determined that there is a memory leak?

This is a good question.

-potential energy is easily made kinetic-

How have you determined that there is a memory leak?

Using the "memory" tab in xcode :

6faa500380aa003e3a29858ff223effe.gif

Add an instance counter (add 1 on construct, subtract 1 on destruct) to some base classes, and print the totals every now and then?

That should give you a rough idea how many objects you have floating around, and whether these are in the expected range.

Add an instance counter (add 1 on construct, subtract 1 on destruct) to some base classes, and print the totals every now and then?

That should give you a rough idea how many objects you have floating around, and whether these are in the expected range.

Not sure what you mean.

I have a std::map of all objects I have, should I just print those out?

You probably have too many objects to make sense of that dump.

I would start by counting how many objects you have of each type, and see what comes up.

Eg:


characters: 28
rooms: 104
sprites: 24502

or whatever kinds of objects you have.

Clearly it's not enough to get to the bottom of your problem this way, but it might give you an idea of where to look more closely.

In C++ the main ways to allocate raw memory are the new operator, the new[] operator, and ::operator new(), each with a nothrow overload. Replacing only one of those will not overload any of the other 5. Chances are good that if you're using the standard library, you using one of the methods other than the one new operator.

Stephen M. Webb
Professional Free Software Developer

You probably have too many objects to make sense of that dump.

I would start by counting how many objects you have of each type, and see what comes up.

Eg:


characters: 28
rooms: 104
sprites: 24502

or whatever kinds of objects you have.

Clearly it's not enough to get to the bottom of your problem this way, but it might give you an idea of where to look more closely.

My code is very small, the only objects I have is a text and a sprite object. One of each.

In C++ the main ways to allocate raw memory are the new operator, the new[] operator, and ::operator new(), each with a nothrow overload. Replacing only one of those will not overload any of the other 5. Chances are good that if you're using the standard library, you using one of the methods other than the one new operator.

I haven't learned to allocate memory in any other way than using the new operator yet, if that's what you mean... (aka I have only used the new operator in my game)

My code is very small, the only objects I have is a text and a sprite object. One of each.

:o what do you need a map of objects for then?

Did you actually check this? Anything you have not actually verified should be considered pure speculation until solidly proven to hold.

Your program is not behaving as you intended, therefore at least some of your ideas what happens do not hold. You should not trust your old ideas until you actually know they are true.

Anyway, with just 2 objects, I would guess the most likely cause is in usage of a library then (assuming you use lots of those). Do you get objects returned from a library, and do you dispose of them in the correct way?

My code is very small, the only objects I have is a text and a sprite object. One of each.

:o what do you need a map of objects for then?

Did you actually check this? Anything you have not actually verified should be considered pure speculation until solidly proven to hold.

Your program is not behaving as you intended, therefore at least some of your ideas what happens do not hold. You should not trust your old ideas until you actually know they are true.

Anyway, with just 2 objects, I would guess the most likely cause is in usage of a library then (assuming you use lots of those). Do you get objects returned from a library, and do you dispose of them in the correct way?

I guess it must be the library.

I'll have to go trough every single function I use to check if it creates a object.

I have a map since I will place more sprites in the future, so I can easily access them by a specific ID :)

This topic is closed to new replies.

Advertisement