473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

new/delete when we have a callback

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
1 1796
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
0
by: Mudcat | last post by:
Howdy, I have a simple combox that I have commands entered into which looks like this: """This is a list with the commands to be run in rotisserie fasion.""" self.comList =...
3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
0
by: Weston Weems | last post by:
Hello... I insert cache items in a method with a dependency and a callback reference. I set a breakpoint in the callback, and it does get fired when I add new file into the folder, but I want to...
0
by: george | last post by:
Hi all - I am trying to use ASP.NET 2.0 to delete a row in an Oracle database table GC_LOG (see following, note that my connection string is fine, everything else like updateCommand is...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
0
by: dudeja.rajat | last post by:
On Sat, Aug 30, 2008 at 2:32 PM, Fredrik Lundh <fredrik@pythonware.comwrote: Fredrik, Thanks so much. That worked. Following this, I can now see that my combo2 has no previous elements and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.