Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory Leak question

Started by antmj2317 Jun 29, 2010 at 2:51 PM 5 replies 1.1k views
Original Post
antmj2317
antmj2317
Object CreateObject(Object *o)
{
///code
///code
///code

return o;
}


I have a question about a function similar to this. Typically when a function returns a value, you'd have a call similar to below...

z = result(x, y);

My question with the above code, is, if I'm storing the address of 'o' somewhere in the function, but don't 'catch' it's return value in a variable, does this create a memory leak?
Zakwayda
Zakwayda
Can you post a complete example showing what you mean? (The code you posted probably won't compile, and it's not really clear - to me at least - what you mean by 'store the address somewhere in the function'.)
antmj2317
antmj2317
Object CreateObject(Object *o)
{
///code
///code
///code

arrayofOs[45] = o;

return o;
}
In the above example, the address of 'o' is stored at index 45 in the array.

Unfortunately I can't post a complete example, the best I can give is the following explanation.

If I were say the following: x = o->GetValue(); x 'catches' or holds' the return value.

If I don't catch the return value, but I'm still 'storing' it's address somewhere, is that considered a memory leak?
SiCrane
SiCrane
Do you remember to delete it later? If so, then it's not a leak. If you don't, it is.
Bimble Bob
Bimble Bob
As long as you have access to a pointer to the memory that you can then use to free up said memory it's fine. Also must say it seems odd that you return the parameter back to the caller which presumably provided the parameter in the first place.
It's not a bug... it's a feature!
iMalc
iMalc
Quote:
Original post by antmj2317
Unfortunately I can't post a complete example, the best I can give is the following explanation.

If I were say the following: x = o->GetValue(); x 'catches' or holds' the return value.

If I don't catch the return value, but I'm still 'storing' it's address somewhere, is that considered a memory leak?
CreateObject is returning a pointer, but the function's return type is object. That's why jyk noted that it does not compile. You may not be able to post the real code, but you should try to post an example that compiles.
Where does GetValue fit into the picture? I.e. where is it called from? You've only posted a snippet of CreateObject. How are these related?

A memory leak is when it is no longer possible to free something that was allocated, or that is not freed before the OS reclaims the used memory as the process terminates.
Burnhard
Burnhard
What's wrong with:

shared_ptr<Object> CreateObject(){    shared_ptr<Object> o(new Object(...));     ...    return o;}


I have an almost rabid dislike of "new" and "delete" in C++ code, from bitter experience :p.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.