v. clear() ?
-
Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!
Yonggoo
-
Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!
Yonggoo
since you didn't use
<
and>
rather than<
and>
respectedly, i cannot know what your vactor contains. so if my answer is not accurate, please fix your question. thestd::vector<>::clear()
function clears the vector, but nowhere calls the objects destructors. if your vector contains objects (likestd::vector<MyType>
), then you manage the life of the objects. callingclear()
will only remove the reference (actually, the copy) of the obect in the vector. if your vector contains pointers (likestd::vector<MyType*>
), you have to be more than careful because memory leak are close to occur; especially if you allocated dynamically usingnew
. Using such vectors, you must first iterate over the vector todelete
all the contained pointers, and only then callclear()
. ps: please also use<pre></pre>
tags when posting code sample so that you keep being formated correctly
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
since you didn't use
<
and>
rather than<
and>
respectedly, i cannot know what your vactor contains. so if my answer is not accurate, please fix your question. thestd::vector<>::clear()
function clears the vector, but nowhere calls the objects destructors. if your vector contains objects (likestd::vector<MyType>
), then you manage the life of the objects. callingclear()
will only remove the reference (actually, the copy) of the obect in the vector. if your vector contains pointers (likestd::vector<MyType*>
), you have to be more than careful because memory leak are close to occur; especially if you allocated dynamically usingnew
. Using such vectors, you must first iterate over the vector todelete
all the contained pointers, and only then callclear()
. ps: please also use<pre></pre>
tags when posting code sample so that you keep being formated correctly
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
toxcct wrote:
so if my answer is not accurate, please fix your question.
Best answer I've heard all day! :-D
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
toxcct wrote:
so if my answer is not accurate, please fix your question.
Best answer I've heard all day! :-D
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
DavidCrow wrote:
Best answer I've heard all day!
if my answer was that good, then some idiot didn't understood it, because it seems that i've been rated badly (once again) :(
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi all, I am not sure of the following code. vector::iterator it; for(it = vecGuiInSockets.begin(); it != vecGuiInSockets.end(); ++it) { (*it)->cleanup_socket(); delete (*it); it--; } vecGuiInSockets.clear(); First of all, what does the clear() clean? Does it just clean the pointers to Socket, or it cleans the object to which the pointer points? Second, is the for() loop correctly done? Thanks!
Yonggoo
I doubt the code will work. It doesn't help that we can see < or > characters in your post! The
it--
is crazy and will cause all types of problems.Steve