|
Actaully that GetResource() is third party fucntion(using DLL) and then GetResourceis decalred as
GetResource( char * res_name, char ** s_value );
So i have to use that.
Anu
|
|
|
|
|
That suggests that the function will allocate the string for you, so you don't need to do it, probably just free it (of course that depends on that 3rd party library). So like this:
void this_is_the_function(...)
{
char *czTagGetVal = NULL;
glg_animation[iPage].viewport.GetResource("\\Tag\\String",&czTagGetVal);
sTagName = (CString)czTagGetVal;
...
}
Be careful with freeing up memory allocated by 3rd party things because if you don't do the de-allocation that is in pair with the allocation you can get unpredictable results.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
Judging by that function signature, GetResource will either allocate a string for you, or return a pointer to a string it stores internally (or statically). In either case, the pointer you pass will be overwritten, and any memory previously allocated that this pointer used to point to will be lost!
So, what you should do is:
1. do not allocate memory yourself!
2. initialize the pointer with NULL instead, before calling GetResource()
3. Check the documentation of GetResource() whether or not it allocates memory that needs to be freed or not. If your delete causes an error, then most likely it shouldn't be deleted, or it already gets deleted by whoever manages these resources.
|
|
|
|
|
Possibilities for Error:
- You've already deallocated this memory block.
- If you allocated within a DLL, you should deallocate within the same DLL. Don't do memory management across DLL boundaries.
Memory Leak:
- If the function GetTagLbl() gets called more than once, you'll have leaking memory by only deleting once when closing.
|
|
|
|
|
Hi all,
i am using CreateFile to open the port,its working fine when i am handle this with only single port.
but now i want to open multiple port at the same time and perform read/write operation on these ports.
In The open port method, it opens the first available port and when it gets the second it closes the opened one and then opens the next port and so on.
if (IsOpen())
{
BOOL bSuccess = CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
if (!bSuccess)
TRACE(_T("Failed to close up the comms port, GetLastError:%d\n"), GetLastError());
}
if (m_hEvent)
{
CloseHandle(m_hEvent);
m_hEvent = NULL;
}
CString sPort;
sPort.Format(_T("\\\\.\\COM%d"), nPort);
m_hComm = CreateFile(sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
so please help me how can i do this.
thanks in advance.
|
|
|
|
|
Are you possibly using the same instance of this class to open all your ports?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
i am using it like this:
void CAppClass::fun1()
{
for(int i=0; i<port_count;i++)
{
CPort_Class* p_c=new CPort_Class;
p_c->Open()
p_c->Close();
}
}
|
|
|
|
|
This code opens a port and then later on closes it before going on with the next one. I thought you wanted to have multiple ports open at the same time. Could you clearify?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
yes i want to open the multiple port at same time.
|
|
|
|
|
The code fragment you have shown opens and closes the ports one-by-one. You should rethink your strategy i guess. Without knowing more about what exactly you are trying to achieve it is hard to guess on anything, but you could for example try strating a new thread for each port if there aren't too many ports you want to use (of course you would need to implement proper synchronization as needed), or you could have an array for the port class instances, something like this:
CPort_Class *ports = new CPort_Class[portcount]
for (int port = 0; port < portcount; port++) ports[port].Open();
...
...
for (int port = 0; port < portcount; port++) ports[port].Close();
delete []ports; This is a simplified code fragment, you would of course need to do error checks too.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
now i am crate different thread for each port,now how can i do this?
|
|
|
|
|
If you don't know threading then you have a lot to learn. There are great articles about threading all around the web so google away. But if you do not know threading yet then maybe the other solution would be better for you, but as said, since i do not know what you are trying to achieve, it is hard to say.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
|
You are probably specifying it an invalid handle or the instance of which you are trying to use its m_hComm member has been deleted OR it never existed at all and you are calling into some random pointer.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Hi experts, I am reading a source file written in C and there is a declaration syntax which is not familiar to me. The source file is from libmad mp3 decoder. When the file in which the declaration is made has a file extension of .c , the c++ compiler is able to compile successfully. I have changed the source file extension to .cpp but the compiler reports an error. When I looked at the line from where the error was reported, I saw an array declaration which I do not understand very well. This is how the array has been declared.
unsigned char (*main_data)[MAD_BUFFER_MDLEN];
The declaration has been made in a structure called mad_stream. I am not certain about the pointer (*main_data) . The following assignment generates an error:
stream->main_data = malloc(MAD_BUFFER_MDLEN);
and Microsoft Visual Studio 2005 reports the following error:
.\mad\layer3.cpp(2531) : error C2440: '=' : cannot convert from 'void *' to 'unsigned char (*)[2567]'<br />
Could someone please explain to me what is happening? What should I do?. Thanks.
|
|
|
|
|
I think this is an array of unsigned char pointers.
unsigned char (*main_data)[MAD_BUFFER_MDLEN]; is somewhat equivalent to:
unsigned char **main_data;
The difference is that in the first declaration above, the entire array is created on the stack by the compiler.
You don't need to allocate this array with malloc since it's already on the stack.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Right Richie, but do you have any idea how I should allocate the memory in C++ with the new operator for it to work? I still can't figure it out.
For instance, I was playing with this syntax in both C and C++. As an example, the following code compiled successfully when the file extension was .c :
#include <stdio.h>
int main(void)
{
char (*ptr)[40];
ptr = malloc(40);
free(ptr);
return 0;
}
However, by just changing the source file to .cpp , the code doesn't compile. Anyway of accomplishing this in C++?
modified on Sunday, July 17, 2011 7:14 PM
|
|
|
|
|
Can you say exactly what error it gives when you say it doesn't compile in C++?
I know why it won't compile in C++, it's because C++ has stricter type checking.
You need to cast the output of the malloc call thus:
*ptr = (char*)malloc( sizeof(char*) * 40 );
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: I think this is an array of unsigned char pointers.
Sorry Richard, you are incorrect. This (awful) syntax needs to be read from inside the parenthesis and then right to left so that:
(*main_data) is a pointer, to [MAD_BUFFER_MDLEN] an array this length of unsigned char s.
The best things in life are not things.
|
|
|
|
|
char (*p)[10]; is declaring a pointer to a fixed size array and is different from char *p[10]; which simply means an array of 10 pointers.
You allocate memory using p = new char[2][10]; and deallocate using delete [] p;
|
|
|
|
|
Superman, thanks for stepping in.
What is the difference between a "fixed size array" and an "array of 10 pointers?"
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
|
Excellent. I just learned something very useful.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I thought I understood this but I am now confused as to the memory allocation. Why new char[2][10] and not just new char[10] ? Isn't p just an array of characters?
The best things in life are not things.
|
|
|
|
|
Take a look at the following examples.
Say I have a function as below -
void fun(char (*p)[10])
{
cout << *p << endl;
} Now you will be able to call it as -
char name[10];
strcpy_c(name, "Hello");
fun(&name); But you will get a compile error if you try something like -
char name[11];
strcpy_c(name, "Hello");
fun(&name); You could have a slightly different version using references -
void fun(char (&r)[10])
{
cout << r << endl;
}
char name[10];
strcpy_c(name, "Hello");
fun(name);
char name2[11];
strcpy_c(name2, "Hello");
fun(name2); So even though the code calling the function looks exactly like it does when using a function accepting a simple character pointer, it is a safe function.
This is exactly how the new safe string functions are implemented.
The advantage here is that you do not need to pass in a second size parameter to the function and the check for the size is done by the compiler at compile time.
But when you allocate memory using new , then it becomes something like -
char (*p)[10] = new char[5][10]; You can now pass this on to a function, but you would need another level of indirection -
void fun(char (**p)[10]){}
fun(&p); Or
void fun(char (*&p)[10]){}
fun(p);
|
|
|
|