By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

new/delete when we have a callback

P: n/a
they give me function pointer for call back;
here is the func;

funcA(void* buffer,int size);

when i receive data i am goinf to call this func, buffer with data and
its size..
i have data listener thread; i receive data like this;

void* d = malloc(100);
recv(d,100);

then i call callback;

cb(d,100);

so; when we put this code in to a loop to receive&call continuesly,
a memory leak occurs... unless this buffer is not freed in callback
func.
Now i am free ing after calling callback... (after line cb(d,100); ),
i hesitate; is this the correct strategy....?

Sep 26 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
xenonysf wrote:
they give me function pointer for call back;
here is the func;

funcA(void* buffer,int size);

when i receive data i am goinf to call this func, buffer with data and
its size..
i have data listener thread; i receive data like this;

void* d = malloc(100);
recv(d,100);

then i call callback;

cb(d,100);

so; when we put this code in to a loop to receive&call continuesly,
a memory leak occurs... unless this buffer is not freed in callback
func.
Now i am free ing after calling callback... (after line cb(d,100); ),
i hesitate; is this the correct strategy....?
Generally, if you allocate something, you should free it (directly, or
indirectly through RAII) or you should make it clear to your clients
that they are responsible for freeing it by using a smart pointer such
as std::auto_ptr. In the case of an array, you should use a container
that will clean itself up such as std::vector. See
http://www.parashift.com/c++-faq-lit....html#faq-34.1.

Cheers! --M

Sep 26 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.