You are not logged in.

1

Saturday, December 17th 2011, 11:20am

converting Qstring to const char * - "invalid null pointer"

hi,
i'm trying to build an test aplication for my DLL and i've got a problem...
1. i'm trying to convert QString from line edit:

Source code

1
2
3
QString message = ui.commandLineEdit->text(); 
 const QByteArray inBytes = message.toAscii(); 
 const char * cStrData = inBytes.constData();


and message is:
Debug Assertion Failed
Program....... \incude\xstring
Line:930
Expression: invalid null pointer

it appears after line

Source code

1
sendRecvData(cStrData, panId, message.length(),1);


i've tried convert toAscii, toLatin1, toUtf8 etc and always the same error. Solutions?

here's the code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
zigbeeapp.h 
#ifndef ZIGBEEAPP_H 
#define ZIGBEEAPP_H 
#include QSignalMapper
#include "ui_zigbeeapp.h" 
#include "ZigBeeLib.h" 

class ZigBeeApp : public QMainWindow 
{ 
 Q_OBJECT 
public: 
 ZigBeeApp(QWidget *parent = 0, Qt::WFlags flags = 0); 
 ~ZigBeeApp(); 
public slots: 
void sendNewMessage(int panId); 
private: 
 Ui::ZigBeeAppClass ui; 
}; 
#endif // ZIGBEEAPP_H

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
zigbeeapp.cpp 
#include "zigbeeapp.h" 
ZigBeeApp::ZigBeeApp(QWidget *parent, Qt::WFlags flags) 
 : QMainWindow(parent, flags) 
{ 
 ui.setupUi(this); 
 int panId=2; 
 QSignalMapper* signalMapper = new QSignalMapper(this); 
    signalMapper->setMapping(ui.sendButton, panId); 
 connect(ui.sendButton, SIGNAL(clicked()), signalMapper, SLOT(map())); 
 connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(sendNewMessage(int)));
}
ZigBeeApp::~ZigBeeApp(){} 
void ZigBeeApp::sendNewMessage(int panId) 
{ 
QString message = ui.commandLineEdit->text(); 
 const QByteArray inBytes = message.toAscii(); 
 const char * cStrData = inBytes.constData(); 
//int addr = ui.addressLineEdit->text().toInt(); 
sendRecvData(cStrData, panId, message.length(),1); 
}


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
part of ZigBeeLib.cpp

bool sendData(const char* pData, int panId, int dataLen, unsigned int addr)
{
	char buf[1024];
	buf[0] = 0x81;   // start delimiter
	buf[1] = strlen(pData);	// data length
	buf[2] = 0x00;   // reserved
	buf[3] = 0x01;	// number of network retries/broadcasts attempts
	buf[4] = 0x00;   // reserved
	if (addr  == 0)
	{
		buf[5] = 0xFF;
		buf[6] = 0xFF;
	}
	else
	{
		buf[5] = (addr >> 8);
		buf[6] = (addr & 0xFF);
	}
	memcpy(&(buf[7]), pData, dataLen);
	unsigned long bytesWritten;
	WriteFile(comTable[panId], buf, (dataLen + 7), &bytesWritten, NULL);
	if (WriteFile==0)
	{
		return false;
	}
	else 
	{
		return true;
	}
}

bool sendRecvData(const char* pData, int panId, int dataLen, unsigned int addr)
{
	sendData(pData, panId, dataLen, addr);
	if (sendData == false)
	{
		return false;
	}
	else
	{
		recvData(panId, addr);
		return true;
	}
}
}

This post has been edited 5 times, last edit by "rdkk" (Dec 18th 2011, 6:02pm)


2

Saturday, December 17th 2011, 12:33pm

so just use your debugger to follow into that piece of code and find the actual line that uses a string and makes the assert. Learning how to use the debugger is not optional for programmers.

You know it happens in sendRecvData, but you didn't show us this code. I suggest you step through that method line by line.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, December 18th 2011, 6:01pm

so just use your debugger to follow into that piece of code and find the actual line that uses a string and makes the assert. Learning how to use the debugger is not optional for programmers.

You know it happens in sendRecvData, but you didn't show us this code. I suggest you step through that method line by line.


im sure there is any problem with sendRecvData and sendData, problem is with converting data from qstring to const char * - error appears when i want to call sendRecvData with converted to const char* qstring and i dont know why...

4

Sunday, December 18th 2011, 6:20pm

maybe the problem isn't inside, but you can see what the value is that is causing the issue, and back track it. Start from the source of the symptom, and work backwards. Let us know how you get on.

have you checked the value of the char* and made sure it isn't ever null?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Dec 18th 2011, 6:27pm)


Similar threads