|
Struct is defined as below. Before crash ,value of cstrFilePath is empty.
struct myStruct
{
int charsize ;
CString cstrFilePath;
myStruct() : charsize (0)
{
cstrFilePath.Empty();
}
} ;
|
|
|
|
|
pandit84 wrote: Before crash ,value of cstrFilePath is empty. Which is rather irrelevant. What I asked was, what is the value of structRef when the crash occurs?
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
You have supplied very little to go on, so in addition to suggesting you provide more information I'll ask a question: are you allocating memory in one module and freeing it in another (a module is a dll or exe)?
Steve
|
|
|
|
|
I am sorry for adding incomplete info.
I have allocated memory to object of structure myStruct in one dll and passed this struct object as a reference to another dll. This crash happens only while assinging value to
structRef.cstrFilePath variable. Finally I was able to assign value inside CString variable using
wsccpy method. I was able to resolve this crash with following code , but I am sure this is not the correct way to do it.
wcscpy (structRef.cstrFilePath.GetBuffer (), str.GetBuffer ()) ;
I am still trying to resolve this issue in a correct way. I am using Windows 7 system and GCC Compiler. Thanks all for replying to my previous post.
|
|
|
|
|
Unless you configure things properly each module (dll or exe or other executable module) has it's own heap. In this environment you can't allocate memory in one module and free it another. Given that CString manages a buffer using an instance in multiple modules could result in this happening. Forget wcscpy, it's ugly and isn't a fix for the root problem, whatever it is. If it's heap corruption, which is my guess, it's going to blow up sooner or later and "fixing" one problem with an ugly hack will just result in it popping up somewhere else.
Steve
modified 5-Dec-12 11:14am.
|
|
|
|
|
Finally I have solved this issue.
The CString when used with GCC was Used as LinuxCString which is internally std::string. Capacity of std::string when calculated was every time 0. so when I tried to assign const char* to LinuxCString ( i.e. CString in MSVC ) it was crashing.
I have used std::string.reserve (200) method to specify the capacity first while initialization. This solves my issue.
I am still evaluating whether my fix is perfect or not.
Thanks all
|
|
|
|
|
The fix is far from perfect, you shouldn't need to call reserve. If I were you I'd be looking for the real problem.
Steve
|
|
|
|
|
Hi, I am developing an application that toggles the screen in Debian 6 Platform.
In earlier days I done this with Debian 5, It works good. But if I use the same code in Debian 6, I face a segmentation fault in libgtk-x11-2.0.so.2000.0 and libgdk-x11-2.0.s0.2000.0. Since they are Library files I cant do anything with that. Is there any other possibility to prevent my application from facing Segmentation fault???
Expecting for a quick reply
Thanks & Regards
Srivathsan
|
|
|
|
|
I don't think anyone is likely to be able to guess why your program fails. You need to do some diagnosis to find out where the segmentation fault occurs, and what the last call out from your code was.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
This is the Snippet I got when tried to trace the Segmentation fault using GDB
#0 0xb7281601 in ?? () from /usr/lib/libgdk-x11-2.0.so.0
#1 0xb7282835 in ?? () from /usr/lib/libgdk-x11-2.0.so.0
#2 0xb7052381 in ?? () from /lib/libglib-2.0.so.0
#3 0xb7054305 in g_main_context_dispatch () from /lib/libglib-2.0.so.0
#4 0xb7057fe8 in ?? () from /lib/libglib-2.0.so.0
#5 0xb7058527 in g_main_loop_run () from /lib/libglib-2.0.so.0
#6 0xb7418b69 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#7 0x080e82a1 in main_callback (data=0x0) at src/dispimage.c:155
#8 0xb707e6cf in ?? () from /lib/libglib-2.0.so.0
#9 0xb77a7955 in init_rotgrids(rotgrids**, int, int, int, double, int, int, int, int) () from /lib/i686/cmov/libpthread.so.0
#10 0xb6f99e7e in clone () from /lib/i686/cmov/libc.so.6
|
|
|
|
|
#7 0x080e82a1 in main_callback (data=0x0) at src/dispimage.c:155
I guess that's your first clue. But really, you have to do some analysis here; we cannot begin to guess what is going on in your code.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I tried out with GDB to detect the place where Segmentation fault Occurs. I cant able to retrieve any local Variable's Information at that place. Is there any good tool to trace Segmentation Fault?
Thanks & Regards
Srivathsan
|
|
|
|
|
SrivathsanRaghavan wrote: Is there any good tool to trace Segmentation Fault? None that I am aware of. Unfortunately they are often difficult to catch without a good back stacktrace. The best you can do is add some extra trace/debug code into your program and see if you can collect some more information before the fault occurs.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I want to set the function pointer at runtime. But i'm stuck here. When i use global function or static class member function, everything is ok. but, when the function is ordinary class member functions. i always got compiler errors. Here is the code:
class A
{
int val;
public:
A() { val = 0; }
A(int j) { val = j; }
int aFun(int k) {val -= k; return val; }
};
typedef int (* func)(int );
class B
{
func m_addr;
public:
B(func param)
: m_addr(param)
{
}
void execute()
{
cout << m_addr(9) << endl;
}
};
I'm trying to use them like this:
A a;
B b(A::aFun);
b.execute();
after googled a lot, i found that std::mem_fun may be helpful. but i don't know how to use it. anyone can help me?
PS: i'm using Visual C++ 2010
|
|
|
|
|
I think the function needs to be a class function (i.e. static ).
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The code doesn't make a lot of sense. The is no connection between the member function to be called and a specific instance of A on which to call it. Also the typedef is incorrect for a non-static member function, make it look likes this:
typedef int (* A::func)(int );
Steve
|
|
|
|
|
A::aFun is different from func.
|
|
|
|
|
When you think about it, what you try to achieve isn't logically possible:
A member function that is not static implicitely takes a pointer to the instance as it's first (hidden) argument, so you cannot encode that as a simple pointer, as you always have to additionally pass that instance-pointer. Class B does not have a pointer to an instance of class A, so it cannot possibly call a member function, whether by pointer or any other mechanic!
When you look at your code, where do you use the object a ? Nowhere!
|
|
|
|
|
|
thank you very much. the command pattern is the perfect solution.
|
|
|
|
|
Hi all,
I am a beginner in networking, I want to do a project to move my arm robot in the server (it is mac) with my p5 glove in the client (it has os window) using client server udp with c++.
In client site I have visual C to translate the movement of the glove.
In server site there are programs to move the robot in c language.
I want to ask, how to send the data/information from the glove to move the arm.
Thank you
daniel
|
|
|
|
|
First off... for something like this, I wouldn't use UDP, I would use TCP/IP. There's really no reason to use UDP, which sends datagrams with no built-in error checking, TCP/IP already handles the error checking/handling (i.e. it'll automatically retransmit packets that didn't arrive at their destination correctly). Usually you only want to use UDP when you have something that doesn't require reliability like voice or video (i.e. if you lose a packet here and there it won't really matter, you'll still be able to hear/see the other person).
As far as sending the data, if you're making for the server and client, it's easy, you define the data messages/structures that are being passed between both completely yourself (via what is typically referred to as an API). Usually the packets are made up of binary buffers, within that buffer, you can either have fixed length or variable length data buffers (or packets), the structure of which is completely up to you.
For example, you can specify:
0. first 4-bytes of the buffer define the message type
1. next 4-bytes define a message specification
2. next 4-bytes define the message size
3. so on...
In this scenario, when you receive a packet, first thing you'll do is cast it onto a data structure that is defined by your API. The first portion of the structure would be an int type (picked it because it's 4-bytes in a 32bit system) and it can specify the type of message that is contained in that data packet. The next portion can be a subset of that message and so on.
Search google and CodeProject for client/server examples and see how they defined their messages.
|
|
|
|
|
I have a CListCtrl with the LVS_EX_CHECKBOXES style and I want to limit the number of items that the user can select. I have written the following code but (of course) it gets itself in a loop because in changing the state of an item, it causes OnItemchangingLoadingValues() to be called again.
void CSpecimenLoadingDialog::OnItemchangingLoadingValues(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
int nCheckedItems = 0;
for(int i = 0; i < m_loadingVals.GetItemCount(); i++)
{
BOOL bState = ListView_GetCheckState(m_loadingVals.m_hWnd, i);
if(bState)
{
nCheckedItems++;
}
}
if(nCheckedItems == 2)
{
ListView_SetItemState(m_loadingVals.m_hWnd, pNMListView->iItem, FALSE, LVIS_STATEIMAGEMASK);
}
*pResult = 0;
}
What is the correct way to go about doing this?
|
|
|
|
|
softwaremonkey wrote: because in changing the state of an item, it causes OnItemchangingLoadingValues() to be called again. But the parameters (i.e., state) passed each time are different.
Rather than use a for() loop each time an item's state changes, simply check the state passed and use a counter. Then in OnItemchangingLoadingValues() , you could have something like:
void CSpecimenLoadingDialog::OnItemchangingLoadingValues(NMHDR* pNMHDR, LRESULT* pResult)
{
if (nCheckedItems < 2)
{
if (pNMHDR's new state equals checked)
nCheckedItems++;
else
nCheckedItems--;
}
}
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
You may add a member variable to your class (or a static variable to the handler function) indicating that the handler is called recursive:
private:
m_bIgnoreChgHandling;
CSpecimenLoadingDialog::CSpecimenLoadingDialog()
{
m_bIgnoreChgHandling = false;
}
void CSpecimenLoadingDialog::OnItemchangingLoadingValues(NMHDR* pNMHDR, LRESULT* pResult)
{
if (!m_bIgnoreChgHandling)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
int nCheckedItems = 0;
if(nCheckedItems == 2)
{
m_bIgnoreChgHandling = true;
ListView_SetItemState(m_loadingVals.m_hWnd, pNMListView->iItem, FALSE, LVIS_STATEIMAGEMASK);
m_bIgnoreChgHandling = false;
}
}
*pResult = 0;
}
|
|
|
|