|
You need to look at the possibility of the function exiting before delete is called. From your code it looks like if an exception is thrown that's what happens; you catch it, and then return without freeing inp.
You may want to consider not allocating inp dynamically since it's always got a constant size (MAXCHARS).
char inp[MAXCHARS];
Another alternative is to dynamically allocate it, but on the stack using alloca:
char* inp = (char*)alloca(MAXCHARS);
Then you don't need to worry about explicitly freeing it since it will happen automatically.
Regards,
Alvaro
All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
|
|
|
|
|
You should look at your routine, and, before EVERY return, you should delete []inp.
You have 3 return points on your routine.
That's why some people advocate that routines should be structured so they have only one point of return. I'm not so rigid at this rule, but surely C++ beginners can benefit from writing routines with only one return point.
I see dumb people
|
|
|
|
|
I agree. A single return is one of the rules I would stick to as a new programmer. After you have been programming for many years, you learn how to safely break the rules such as "single return" or "no goto".
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I was drawn ploygon using Mouse on a DialogBox(not by Graphics commands of polygon), now i want to save this drawing to a file and restore it to back, how can i do this ?
Thank you
-Umakanth
|
|
|
|
|
an example (but i can't remember its name) in msdn (first lesson about MFC) tells exactly what u need.
the example includes several steps, about using collections and how to save and open data.
includeh10
|
|
|
|
|
if I Have DLL File And I want to use it in my project ...
How I Can Know How many function in this DLL File ,
what are They ? and What are the parameter for each Function in it and data type of them ?
Many Many Thanx for response
Hamoud Alhuttami
Yemen - Sana'a
|
|
|
|
|
open the dll as buffer, the info u need can be viewed from that buffer.
i don't know format of a dll file, but i did it before: as u want to know now.
note: the buffer is in a byte file (including zero in middle), but function's info is in asc ii format.
possile need no more than 1 day to go.
includeh10
|
|
|
|
|
|
1st thank u very much
I wrote the following code to read dll :
(vb code )
dim b as byte
dim c as string*1
open "xx.dll" for random as #1 len=len(b)
open "xx.txt" for random as #2 len=len(c)
while not eof(1)
get #1,,b
c=chr(b)
put #2,,c
wend
close 1
close 2
then I opened xx.txt file to discovering this dll
i read name of functions but the file is not in text format there are symbols blanks ... try this code
it's no help me
suggest ...
hamoud
huttami@hotmail.com
|
|
|
|
|
1st thank u very much
I wrote the following code to read dll :
(vb code )
dim b as byte
dim c as string*1
open "xx.dll" for random as #1 len=len(b)
open "xx.txt" for random as #2 len=len(c)
while not eof(1)
get #1,,b
c=chr(b)
put #2,,c
wend
close 1
close 2
then I opened xx.txt file to discovering this dll
i read name of functions but the file is not in text format there are symbols blanks ... try this code
it's no help me
suggest ...
hamoud
huttami@hotmail.com
|
|
|
|
|
I have the following class
class DynamicArray {
private:
int* m_pInts;
public:
DynamicArray(int elements) {
m_pInts = new int[elements];
}
~DynamicArray() {
delete [] m_pInts;
}
};
This class creates an dynamically sized array of ints.
I also have the following function
DynamicArray GetDynamicArray() {
DynamicArray temp(89);
return temp;
}
unfourtunatly the destructor is called (on temp) when the function returns, which deletes my array of ints.
example:
int main() {
DynamicArray myArray = GetDynamicArray();
}
How do I make this work?
|
|
|
|
|
Joshua Nussbaum wrote:
How do I make this work?
a) implement a copy ctor (the dtor will be still called for your local instance after it has been copied)
-or-
b) don't use local instance of your class, use a reference
void CreateMyDynamicArray(DynamicArray& ref) { ref.Create(89); }
Last line assumes you have an empty default ctor and a Create(int size) method.
PS: I suggest using a container (e.g. std::vector ) instead of pointer/new/delete
|
|
|
|
|
I agree with moak. Use std::vector and be done with it. To see the std::vector and dynamic data array in action, take a look @ my article and demo project, CDataFile[^].
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Can u plz help me out in capturing a TCP packet(Using Winsock2). Plz provide me with the relevent sites/api names/specific functions to do the job.
Reply me soon
|
|
|
|
|
For capturing the packet through Winsock2 look for the "raw sockets" (search for SOCK_RAW in MSDN). This feature is available in w2k and winXP only I guess.
The better way is to go through building the intermediate driver (NDIS), here you can intercept all packets (but the complexity is quite higher). And also you will not receive tcp traffic, but the raw ethernet frames - this gives you more power, but also more work.
Another approach is to use the Filter-Hook driver. This I don't know very well, but possibly it might be the way. For starter here, might be usefull http://www.codeproject.com/useritems/DrvFltIp.asp[^]
Hope this helps...
|
|
|
|
|
If you are willing to settle for libpcap type of reading the wire you might want to consider looking at the winpcap libraries. You could in theory port routines from strong IDS systems (like Snort for example) with very little problem. You can check out winpcap over at http://winpcap.polito.it/[^] and use of the of examples to get the framework in place.
Once the framework is in place you can hook the filters to do your IDS bidding.
Good luck,
SilverStr
|
|
|
|
|
Hello,
This is a continuation of my earlier question. I've reposted because I have more information and my previous post was a bit misleading. I found that my application crashes during the "LoadFrame" statement below located in MyApp.cpp.
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
It seems that my DialogBar's controls are updating themselves before LoadFrame returns and the next statement executes (m_pMainWnd = pMainFrame). When these controls update, I'm trying to get information from the (MDI) Document to fill them, and the program crashes upon the execution of the following CDialogBar code (because m_pMainWnd is null at the time):
CGoalsDoc* pDoc = (CGoalsDoc*)((CMainFrame *)AfxGetApp()->m_pMainWnd)->GetActiveFrame()->GetActiveDocument();
Why are my controls trying to update (e.g., OnKillFocusEditBox1) before the main frame has been created and "m_pMainWnd" has a value? Is there an easy way around this? I don't think I monkeyed with the MFC Wizard too much, but I may have changed something small, yet important.
Thanks!
JennyP
|
|
|
|
|
Does
m_pMainWnd = new CMainFrame();
if ( !static_cast < CMainFrame* > (m_pMainWnd)->LoadFrame(IDR_MAINFRAME) )
{
return FALSE;
} work?
This way you have a non-NULL m_pMainWnd before LoadFrame() executes.
Alternatively, you can wrap your GetActiveDocument call in a
if (initialised)
clause and set a bool initialised to true after you have loaded the MainFrame.
"My opinions may have changed, but not the fact that I am right."
Found in the sig of Herbert Kaminski
|
|
|
|
|
After some more time, I found the issue (loosely related):
After deleting a control from my dialog, VC++ assigned the first tab stop to a edit control. I handle the kill-focus message for this edit box and this handler crashes the program because it tries to access the document directly.
Therefore, the fix was to change my tab order so that THIS control was not the first.
Good lesson, but it took too much time to debug for me.
JennyP
|
|
|
|
|
Hi, is there a message when a button is clicked "half"?
I mean when you click a button, but release mouse button while not being over the button. As a result the button has the focus, but of course a ON_BN_CLICKED is not send.
Can I handle this case? I want to give the focus back to the previous control. Do I need to catch mouse messages or is there a button message I can handle?
Thx, Moak
|
|
|
|
|
I have used the WM_LBUTTONDOWN in this case and then compared the mouse position with the button's rectangle (easily obtained from CButtonCtrl) to see if the user clicked in the button you want. There may be an easier way, but that's one method.
JennyP
|
|
|
|
|
or as alternatives... if I don't get a message from the button:
2) How about handling WM_LBUTTONUP once and check GetFocus(). Code must be added to PreTranslateMessage() handler of dialog/view.
3) Send a notify from the button to the parent, if button is released but not full clicked. Requires a class derived from CButton, also dialog/view has to handle WM_NOTIFY .
4) Creating a button that can NOT gain focus... like a toolbar button. I think this approach sounds best (and reuseable), anyone with some comments?
|
|
|
|
|
I want to delay 1 second between two action.
How to implement the function?
My so naive implementation looks like this, would you give clever implementation?
delay(int delay_in_second)
{
for(int i=0; i<=delay_in_second; i++)
{
for(int j=0; j<=10000; j++)
{
;
}
}
}
|
|
|
|
|
You would normally use the Sleep() function but this may not be precise enough if you are writing a real-time controller...
he he he. I like it in the kitchen! - Marc Clifton (on taking the heat when being flamed)
Awasu v0.4a[^]: A free RSS reader with support for Code Project.
|
|
|
|
|
You definatly should not use a loop! You might get it perfectly timed on your machine, but as soon as the software is used on a faster CPU then the loop takes less time to perform, and your program messes up big time.
As already identified, you can use Sleep if accuracy to seconds is required. Any more accuracy than that then use the function SleepEx, which takes microseconds, and also allows you to be interrupted. Of course, it all depends on how your program is constructed and what its doing - I dont think a user would appreciate the interface locking up for a second!
|
|
|
|