0

is there something i failed to see?

My project crashes everytime when it come to this function:

#define SMW 29
float smw

getWert(&smw, SMW, rsq,rrq)

SMW ist the Number of the port (kanal), i want to get "smw"

int MainWindow::getWert(float *erg, int kanal, mqd_t rsq, mqd_t rrq)
{
   char *buff;
   struct mq_attr attr;

   struct mqs qmsg;
   qmsg.msg=GETDATA;
   qmsg.dat.kanal=kanal;
   qmsg.dat.typ='d';

   mmqueue mq;

   mq.sdcmd(rsq,(char *)&qmsg, sizeof(qmsg));

   mq_getattr(rrq, &attr);
   buff = (char *)malloc(attr.mq_msgsize);
    mq_receive(rrq, (char *)buff, attr.mq_msgsize, NULL);
   *erg=atof(buff);
   //qDebug()<<"GetWert"<<*erg;
   return 1;

}

attr.mq_msgsize = 8192

global.h:

struct mqs{
  unsigned int msg;
  char mess[30];
  kanaldef dat;

mmqueue.h:

class mmqueue
{
public:
    int sdcmd (mqd_t sq, char *buf, int len);
};
QtNewbie
  • 1
  • 3
  • You failed to show how you're calling this function. You're passing a pointer to float, and no one knows if that pointer is valid or not. Second, you have a memory leak in that function. Third, why are you using `malloc` in a C++ program, as well as the superfluous usage of the keyword `struct`? – PaulMcKenzie Sep 19 '14 at 01:54
  • Thanks for your comment. I added the calling of this function. Can you explain where the memory leak is? Third, you should see that i'm not a professional software developer, is that some kind of criminal here? I want to improve my c++ skills but your last questions doesn't help – QtNewbie Sep 19 '14 at 09:44
  • The memory leak occurs because you called `malloc` without calling `free`. When that function returns, you have a memory leak. – PaulMcKenzie Sep 19 '14 at 14:56
  • Please post or give a link to what the members of `mqs` struct are. This call looks suspicious to me: `mq.sdcmd(rsq,(char *)&qmsg, sizeof(qmsg));` You are passing the address of `qmsg`, and that doesn't seem right. Can you also post what the parameter types are supposed to be to that sdcmd function? – PaulMcKenzie Sep 19 '14 at 15:03
  • Thank you very much! I will think about this function malloc. And i added information to struct mqs and the sdcmd function. – QtNewbie Sep 19 '14 at 16:02
  • ok. I made a mistake and assumed that the function call was suspicious. What you should do is run the debugger (you need to learn how to do this), and concentrate on these lines to see what happens when they're executed: `mq.sdcmd(rsq,(char *)&qmsg, sizeof(qmsg)); mq_getattr(rrq, &attr); buff = (char *)malloc(attr.mq_msgsize); mq_receive(rrq, (char *)buff, attr.mq_msgsize, NULL); *erg=atof(buff);` – PaulMcKenzie Sep 19 '14 at 16:53
  • There is a potential for things to go wrong: 1) The mq.sdcmd call may fail. 2) The mq_getattr call may fail. 3) The malloc call could fail, either due to low memory, or the `attr.mq_msgsize` parameter is a bogus value. 4) The mq_receive call could have failed. 5) The call to atof() is being given a string that is totally off-the-wall, and thus causes something in that function to cause a crash. – PaulMcKenzie Sep 19 '14 at 16:55
  • What you should do is a) debug your code using the debugger, b) check your functions to see if they return an error code. Right now, you're just assuming that the functions just "work" without checking for any error codes. Read the documentations on the functions. One glaring error is that you didn't check attr.mq_msgsize to see if it is valid. If it isn't valid, then you are giving `malloc` a bogus value, causing it to possibly return NULL. – PaulMcKenzie Sep 19 '14 at 16:59
  • Ok, thanks very much. I'll try the thing with the debugger as you suggested. And i'll see if i could check attr.mq_msgsize – QtNewbie Sep 19 '14 at 17:25

0 Answers0