|
led mike wrote: I already replied that 90 minutes ago in the C++/CLI forum so he must not think that is it.
99.999% of the time, things like this are simply not realizing you called delete somewhere. I don't check the C++/CLI forum, otherwise I would have yelled at him for that ...
led mike wrote: That should have no effect.
Should, but I have run into cases where doing that actually returns a function pointer instead of an object. Its more that they aren't necessary, so why put them there.
led mike wrote: Second time today someone cross-posted and ignored my reply to their first post. I guess I should take the hint.
It happens. I personally love when they do that then then realize 3 months later that you gave them the right answer the first time.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I havent deleted somefoo (at least I dont think I have). Below is a code snippet.
somefoo *gSomefoo;
class somefoo {...}
somefoo* somefoo::GetInstance (CString fooType){
somefoo *medDevice = (somefoo *) new derivedSomefoo (fooType);
}
void InitParams(){
somefoo fooFactory;
gSomefoo = fooFactory.GetInstance(fooType);
}
fooFactory is a local copy in InitParams, and I see that destroyed as expected. But gSomefoo is global and pointer returned from fooFactory.GetInstance should be valid. What's happening is later in code, another call to new (char *buff = (char*) new char [buffSize]) is returning the pointer of gSomefoo and not a new pointer. Just weird.
BTW, this is all in a DLL, that happens to be loaded/unloaded regularly. gSomefoo has to be global instead of a class member variable for a variety of reasons.
Thanks.
-C
|
|
|
|
|
How are you linking with the C Runtime? Try linking both the exe and Dll with the DLL version instead of static version of MSVCRT.
Thanks
W.
|
|
|
|
|
Wheatbread wrote: another call to new (char *buff = (char*) new char [buffSize]) is returning the pointer of gSomefoo and not a new pointer. Just weird.
Wheatbread wrote: BTW, this is all in a DLL, that happens to be loaded/unloaded regularly. gSomefoo has to be global instead of a class member variable for a variety of reasons.
The part about the DLL being loaded/unloaded would have helped point out your problem much earlier. Load the DLL once and leave it in memory until you close the application. I'll bet you won't see this behavior happening then.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I wish I could load it once. Its not possible with our implementation. Its loaded at startup, but a few various actions in the app causes it to be unloaded, then loaded again.
Is there any advantage in making the function GetInstance a static class function? Or making the global variable static?
-C
|
|
|
|
|
Methods like GetInstance are usually used in the singleton pattern, in which case all constructors for the object are private and you keep a pointer to the instance in the class itself (if it is null, you create a new one, otherwise you just return the pointer to the object that already exists).
Fixing that, however, will not fix the problem you are seeing. Knowing that you are loading/unloading the DLL constantly, my guess is that somewhere you create the object, the DLL is unloaded (thereby releasing any memory it had allocated and invalidating it), and then you declare a character array that happens to get the same address.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Is there anyway a file is saved or not ? MSDN doesn't tell much on that
hitherto shall thou come but not further thee
|
|
|
|
|
what? How is it a file if it is not saved?
Nice try deleting your post and reposting in this forum.
|
|
|
|
|
nope I meant if the file is already created and the file has been changed by some other editor but the file has not yet been saved .Is there a way i can get all the file descriptors opened for a given file?
OOps that was unintentional i wawnted to post it on this forum itself.
hitherto shall thou come but not further thee
|
|
|
|
|
napoleaninlondon wrote: nope I meant if the file is already created and the file has been changed by some other editor but the file has not yet been saved .Is there a way i can get all the file descriptors opened for a given file?
I don't think so from an external point of view; unless the file is still opened, then you could assume that the file will be modified.
but if an application loads a file data and then close the file, and makes some modification to the data, you cannot know that from the "outside"
|
|
|
|
|
Maximilien wrote: but if an application loads a file data and then close the file, and makes some modification to the data, you cannot know that from the "outside"
How about the GetFileAttributesEx() set of functions through which you are able to fill a WIN32_FILE_ATTRIBUTE_DATA struct. You can then check the FILETIME ftLastAccessTime; FILETIME ftLastWriteTime; members for the dates and times.
|
|
|
|
|
napoleaninlondon wrote: Is there a way i can get all the file descriptors opened for a given file?
Even if you could they would not tell you if the "other" application intends to write to the file in the future. I am afraid your current view of this does not appear logical. You might need to step back and reframe the requirements and goals of the project.
led mike
|
|
|
|
|
|
|
Hai all,
Can anyone tell how to convert LPTSTR to char*
Thanks in advance
George K Jolly
|
|
|
|
|
Depends on what you are compiled for. If Ansi just cast it otherwise you have to use a conversion function or one of the macros like CT2A etc.
led mike
|
|
|
|
|
By using a type-cast. What code is giving you trouble?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
If UNICODE is not defined (that is, an ANSI build), LPTSTR is a char*. If UNICODE is defined, you can use the T2A macro from ATL, or use _bstr_t (or various other means) to end up with a char*.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
CGenericList.Clear method should clear the contents of m_Data from the memory heap. This is done because strdup is called to create a duplicate of the string in a linked list so that data is not overwritten.
However, should CGenericList be of type int the Clear method would not compile since it does not take int types as parameters:
GenericList.h(32): error C2664: 'free' : cannot convert parameter 1 from 'int' to 'void *'
SentenceList.cpp
---------------------------------------------------------------------
CSentenceList::CSentenceList(char strFileToRead[MAX_SENTENCE_LENGTH + 1], bool bSorted)
: CGenericList<char*> ()
...
void CSentenceList::readFile(char strFileToRead[MAX_FILENAME_LENGTH])
{
char strLine[MAX_SENTENCE_LENGTH + 1];
fstream fileToRead(strFileToRead,ios::in);
while (!fileToRead.eof() && Size < MAX_LINES)
// Read line of text from file and update Sentences.strSentence.
{ fileToRead.getline(strLine, MAX_SENTENCE_LENGTH);
Sentences.Insert(strdup(strLine));
Size++;
}
fileToRead.close();
}
...
GenericList.h
---------------------------------------------------------------------
template<class T>
class CGenericList
void Clear() // empties the list
{
CGenericNode<T> *pTemp = m_pFirst;
// clear the data
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
free (pTemp->m_data);
pTemp = pRemember;
}
// clear the nodes
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
delete pTemp;
pTemp = pRemember;
}
m_iCount = 0;
}
Is it wise and possible to say, for example:
if typeof(<T>) == char*
// Clear the data
...
free (pTemp->m_data);
...
Alternatively how can I loop through the methods from the derived class, in this case, CSentenceList?
Jon
|
|
|
|
|
jon_80 wrote: while(pTemp != NULL)
{
CGenericNode<t> *pRemember = pTemp->m_pNextNode;
free (pTemp->m_data);
pTemp = pRemember;
}
First, it doesn't look like you are allocating your data variable on the heap, so there is no need to delete it.
Second, it is generally unwise to use 2 different heaps like this (malloc/free is not the same heap as that of new/delete).
Third, in general, when writing generic classes like this, you accept any type and just hold it. If your link list is holding pointers (e.g. char*), it is up to the module that created those pointers to delete them. This is especially true in this case since if you pass a pointer to a char* that was created with new and try to free it using free instead of delete, you will corrupt memory.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Regarding the third point it makes sense from a design point of view, but, how can I free up memory from the calling module?
The memory heap is allocated as follows:
...
while (!fileToRead.eof() && Size < MAX_LINES)
// Read line of text from file and update Sentences.strSentence.
{ fileToRead.getline(strLine, MAX_SENTENCE_LENGTH);
Sentences.Insert(strdup(strLine));
// string is copied, this is where I think I am allocating memory.
Size++;
}
...
The problem is that protected members and public members cannot be called from derived classes (somehow!!). Can I send u the code somewhere?
Jon
|
|
|
|
|
First of all, this is one of the reasons why a few people recommended you use the standard string class, but I digress.
You're list class (which I believe you called GenericList), should have some accessor (either a get method, or iterators) to the data it holds. Whatever module is using the above code (the one calling strdup) should also have a cleanup method (or simply the destructor) that will iterate through the list and call free on each data element prior to emptying the list (which the list class itself should take care of).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
The weird thing is that I'm getting access related errors when I'm trying to clean-up from the calling class.
...
CSentenceList::~CSentenceList()
{
// Clear the data
CGenericNode<char*> *pTemp = Sentences.m_pFirst;
while(pTemp != NULL)
{
CGenericNode<char*> *pRemember = pTemp->m_pNextNode;
free (pTemp->data);
pTemp = pRemember;
}
}
...
c:\Documents and Settings\Jon\My Documents\Visual Studio Projects\IAD Practice and Assignment\C++ - Assignment\02 & 03 - Sentence\SentenceClass\SentenceList.cpp(19): error C2248: 'CGenericList<T>::m_pFirst' : cannot access private member declared in class 'CGenericList<T>'
with
[
T=char *
]
and
[
T=char *
]
c:\Documents and Settings\Jon\My Documents\Visual Studio Projects\IAD Practice and Assignment\C++ - Assignment\02 & 03 - Sentence\SentenceClass\SentenceList.cpp(22): error C2248: 'CGenericNode<T>::m_pNextNode' : cannot access private member declared in class 'CGenericNode<T>'
with
[
T=char *
]
and
[
T=char *
]
CGenericList.h
#pragma once
#include ".\GenericNode.h"
#include <iostream>
#include <stdlib.h>
#include <tchar.h>
using namespace std;
template<class T>
class CGenericList
{
public:
CGenericList(void)
{
m_pFirst = NULL;
m_pLast = NULL;
m_iCount = 0;
}
virtual ~CGenericList(void)
{
Clear();
}
void Insert(T pData)
{
// create the node to store our data
CGenericNode<T> *pNode = new CGenericNode<T>;
pNode->data = pData; // set the payload
if(m_iCount == 0)
{
m_pFirst = pNode;
m_pLast = pNode;
}
else
{
m_pLast->m_pNextNode = pNode;
m_pLast = pNode;
}
m_iCount++;
}
bool Find(T pData)
{
CGenericNode<T> *pTemp = m_pFirst;
while(pTemp != NULL)
{
if(pTemp->data == pData)
return true;
pTemp = pTemp->m_pNextNode;
}
return false;
}
bool Remove(T pData)
{
CGenericNode<T> *pTemp = m_pFirst;
CGenericNode<T> *pPrevious = NULL; // keep track of previous node
while(pTemp != NULL)
{
if(pTemp->data == pData)
{
// we found the node!
RemoveNode(pPrevious, pTemp); // call helper method
return true; // true indicates that something was removed
}
pPrevious = pTemp; // remember the previous node
pTemp = pTemp->m_pNextNode;
}
return false; // we found nothing here
}
int GetCount()
{
return m_iCount;
}
void Display()
{
CGenericNode<T> *pTemp = m_pFirst;
int iCount = 0;
while(pTemp != NULL)
{
iCount++;
cout << iCount << "|";
cout << pTemp->data << _T(" ");
cout << endl;
pTemp = pTemp->m_pNextNode;
}
}
protected:
int m_iCount;
CGenericNode<T> *m_pFirst;
CGenericNode<T> *m_pLast;
// helper method
void Clear() // empties the list
{
CGenericNode<T> *pTemp = m_pFirst;
// clear the nodes
while(pTemp != NULL)
{
CGenericNode<T> *pRemember = pTemp->m_pNextNode;
delete pTemp;
pTemp = pRemember;
}
m_iCount = 0;
}
void RemoveNode(CGenericNode<T> *pPrevious, CGenericNode<T> *pTemp)
{
if(pPrevious == NULL) // remove the first node
{
m_pFirst = pTemp->m_pNextNode;
if(m_pFirst == NULL) m_pLast = NULL; // this was the only node
delete pTemp; // delete the original node
}
else
{
pPrevious->m_pNextNode = pTemp->m_pNextNode; // skip this node
if(pPrevious->m_pNextNode == NULL) // in case we are deleting the last node
m_pLast = pPrevious;
delete pTemp;
}
m_iCount--;
}
};
CGenericNode.h
#pragma once
#include <stdlib.h>
// forward declaration of CGenericList (it is a template)
// and is a friend of CGenericNode
template<class T> class CGenericList;
// declaration of CGenericNode
template<class T>
class CGenericNode
{
friend CGenericList<T>;
public:
CGenericNode()
{
m_pNextNode = NULL;
}
T data;
//protected:
// T m_data;
private:
CGenericNode<T> *m_pNextNode;
};
Jon
|
|
|
|
|
Hello,
I received an error message when i tried to send serial port the datas with outportb.
%Starting with dos.h library
...
int port=0x378;
outportb(int port, unsigned int);
...
...
for ( i=0 ; i<8 ; i++)
{
outportb(port,member.u8value[i]);
}
...
Error Message ==> [Linker Error] Unresolved external '_outportb' referenced from H:\INTERFACE\INTERFACE.OBJ
(Interface is the file in which all datas are saved)
What could be the problem? Thanks for your tricks in advance.
Regards,
Cahit
|
|
|
|
|
In Windows NT and later, you cannot directly access serial ports.
But check this article[^]
See Part 5 for a link to the source to inpout32.dll, which can be used in NT +
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|