Alright, so I'm practicing with C++ classes and pointers. When suddenly...
If I try to get the address of my object passed in the parameters, it gives me an invalid address.
What is going on here?
P.S You can ignore the if statement, it shouldn't be relevant to this particular problem.
Picture of the Problem
.
int ChainLinkz::chainCount = 0;
ChainLinkz::ChainLinkz(int i) {
data = i;
chainID = chainCount++;
fLink = this;
addr = this;
nLink = 0;
printf("Created Link with Value: %i Addr: %x.\n", data, this);
}
ChainLinkz *ChainLinkz::next() {
if (nLink)
return nLink;
else
return 0;
}
ChainLinkz *ChainLinkz::last() {
ChainLinkz * lastLink = fLink;
while (lastLink->next()) {
lastLink = lastLink->next();
}
return lastLink;
}
ChainLinkz ChainLinkz::addLink(ChainLinkz link) {
if (link.nLink) {
if (&link == link.fLink) { // Replace fLink with new address in all instances of previous chain | Fix the old chain
ChainLinkz *t_link = link.nLink;
while (t_link->next()) {
t_link = t_link->next();
t_link->fLink = link.nLink;
}
} else { // Replace nLink for previous link | Fix the old chain
ChainLinkz *t_link = link.fLink;
while (t_link != &link)
t_link = t_link->next();
t_link->nLink = link.next();
}
}
last()->nLink = link.addr;
printf("\n&link: %x | Real Addr: %x \n\n", (ChainLinkz*)&link, link.addr);
link.nLink = 0; //Update values to new chain
link.fLink = fLink;
return link;
}
ChainLinkz ChainLinkz::addLink(ChainLinkz *link) {
printf("Overloader: %x.\n", link);
return addLink(*link);
}
ChainLinkz ChainLinkz::operator>>(ChainLinkz link) {
return addLink(link);
}
ChainLinkz ChainLinkz::operator>>(ChainLinkz *link) {
printf("Overloader: %x.\n", link);
return addLink(*link);
}
Solved the problem by using references instead of passing the object itself.
ChainLinkz ChainLinkz::addLink(ChainLinkz &link) {
printf("Overloaded New: %p\n", &link);
return addLink(&link);
}
ChainLinkz ChainLinkz::addLink(ChainLinkz *link) {
if (link->nLink) {
if (link == link->fLink) { // Replace fLink with new address in all instances of previous chain | Fix the old chain
ChainLinkz *t_link = link->nLink;
while (t_link->next()) {
t_link = t_link->next();
t_link->fLink = link->nLink;
}
}
else { // Replace nLink for previous link | Fix the old chain
ChainLinkz *t_link = link->fLink;
while (t_link != link)
t_link = t_link->next();
t_link->nLink = link->next();
}
}
last()->nLink = link;
link->nLink = 0; //Update values to new chain
link->fLink = fLink;
return *link;
}
ChainLinkz ChainLinkz::operator>>(ChainLinkz &link) {
return addLink(&link);
}
ChainLinkz ChainLinkz::operator>>(ChainLinkz *link) {
printf("Overloaded: %p.\n", link);
return addLink(link);
}
Related
I am making a music player program using MFC.
I would like to add a function to select multiple values in the listbox and delete them, but I would like to ask for advice.
The option of the listbox is Extended, and multiple selection using the control keys is fine.
Among the methods of listbox, it seems to be possible to use SetSel, GetCursel, GetSelItems, etc, but I can't solve it.
I would appreciate it if you could give me a simple advice.
Thank you very much.
void CMFC_MP3Dlg::DeleteList(CStatus* head)
{
CFileFind finder;
CString strTemp;
CString strRemoveFile;
POSITION pos;
CStatus* pTemp = new CStatus();
CStatus* pPrev = new CStatus();
//int nSelCount = m_ListBox.GetCurSel();
//m_ListBox.GetText(nSelCount, strTemp);
LPINT lpSelItems = new int[m_nListBoxCount];
m_ListBox.GetSelItems(m_nListBoxCount, lpSelItems);
for (int nCount = 0; nCount <= m_nListBoxCount; nCount ++)
{
if(m_ListBox.SetSel(nCount,1))
{
m_ListBox.GetText(nCount, strTemp);
m_vec.push_back(strTemp);
}
}
std::vector<CString>::iterator iter;
for(iter = m_vec.begin(); iter != m_vec.end();)
{
strRemoveFile.Format(_T("C:\\MFC_MP3\\%s"), *iter);
BOOL bRet = finder.FindFile(strRemoveFile);
if(bRet)
{
if(DeleteFile(strRemoveFile))
{
pPrev = NULL;
pTemp = head;
if(pTemp == NULL)
{
return;
}
while(pTemp->m_strFileName != strTemp)
{
pPrev = pTemp;
pTemp = pTemp->m_right;
}
if(pTemp->m_strFileName == strTemp)
{
pPrev->m_right = pTemp->m_right;
pTemp->m_right->m_left = pPrev;
delete pTemp;
}
MessageBox(_T("삭제 성공!"));
ShowList();
}
else
{
MessageBox(_T("삭제 실패!"));
}
}
else
{
MessageBox(_T("File Not Found!"));
}
iter++;
}
}
Does this help?
void CChristianLifeMinistryPersonalCopiesDlg::BuildSelectedArray()
{
int i, iSize, *pIndex;
CString strText;
m_aryStrPublishers.RemoveAll();
// get selected count
iSize = m_lbPublishers.GetSelCount();
if (iSize > 0)
{
pIndex = new int[iSize];
m_lbPublishers.GetSelItems(iSize, pIndex);
for (i = 0; i < iSize; i++)
{
m_lbPublishers.GetText(pIndex[i], strText);
m_aryStrPublishers.Add(strText);
}
delete[] pIndex;
}
}
This works fine for me.
Note that your code is incomplete though. We don't see you specify what the count is of the selected items. And we don't see you reset your vector array before you begin.
Have you actually debugged your code to see where it fails?
I have written an Arduino library in C++ that contains an iterator class. If I iterate through it using the same instance all the time, it works as expected. If I create a second instance to do so, it will double the amount of stored objects.
WayPointStack wps = *(new WayPointStack());
wps.AddWP(1, 20);
wps.AddWP(2, 420);
WPCommand c1 = wps.GetNextWP(); // Stack length: 2, correct
c1 = wps.GetNextWP(); //
WPCommand c1 = wps.GetNextWP(); // Stack length: 4, not correct
WPCommand c2 = wps.GetNextWP(); //
WPCommand WayPointStack::GetNextWP()
{
Serial.println("Pointer = ");
Serial.println(pointer);
Serial.println("Length = ");
Serial.println(_length);
if (pointer < _length){
pointer++;
return _wp[pointer-1];
}
return *(new WPCommand(_END, 10000));
}
void WayPointStack::AddWP(int target, int time)
{
if (_length == arrSize)
return;
_wp[_length] = *(new WPCommand(target, time));
_length++;
}
WayPointStack::WayPointStack()
{
_wp = new WPCommand[arrSize];
_length = 0;
pointer = 0;
}
WPCommand::WPCommand(int target, int time)
{
_target = target;
_time = time;
}
Can someone explain this to me?
WayPointStack wps = *(new WayPointStack());
must be
WayPointStack wps;
because it is enough and that removes the memory leak
In
WPCommand WayPointStack::GetNextWP()
{
...
return *(new WPCommand(_END, 10000));
}
you create an other memory leak, may be do not return the element but its address allowing you to return nullptr on error ?
/*const ?*/ WPCommand * WayPointStack::GetNextWP()
{
Serial.println("Pointer = ");
Serial.println(pointer);
Serial.println("Length = ");
Serial.println(_length);
if (pointer < _length){
return &_wp[pointer++];
}
return nullptr;
}
else use a static var :
WPCommand WayPointStack::GetNextWP()
{
...
static WPCommand error(_END, 10000);
return error;
}
In
void WayPointStack::AddWP(int target, int time)
{
if (_length == arrSize)
return;
_wp[_length] = *(new WPCommand(target, time));
_length++;
}
you create an other memory leak, you just need to initialize the entry :
void WayPointStack::AddWP(int target, int time)
{
if (_length == arrSize)
return;
_wp[_length]._target = target, time));
_wp[_length]._time = time;
_length++;
}
you do not signal the error when you cannot add a new element, what about to return a bool valuing false on error and true when you can add :
bool WayPointStack::AddWP(int target, int time)
{
if (_length == arrSize)
return false;
_wp[_length]._target = target;
_wp[_length]._time = time;
_length++;
return true;
}
Finally Why do you not use a std::vector for _wp
It looks like you have a memory leak on this line:
return *(new WPCommand(_END, 10000));
It looks like you are creating WPCommand on heap, then throw away pointer and return a copy !!!
The example is not minimal and complete so it is hard to give better pointers.
I use the JID3 library which works fine for IDv2_3 but when it is asked to update a tracks tags which are V2_4, it simply wipes the Id3v2_4 tags. How can I test which version of tagging a track has.
Example of setting the ratings and times played in the POPULARIMETER tag
public String setmp3RatingTag(Context context, File SourceFile, String email, int rating, int timesPlayed) throws Exception {
String error = null;
if (timesPlayed < 0) {
timesPlayed = 0;
}
try {
MediaFile MediaFile = new MP3File(SourceFile);
ID3V2_3_0Tag ID3V2_3_0Tag = (org.blinkenlights.jid3.v2.ID3V2_3_0Tag) MediaFile.getID3V2Tag();
POPMID3V2Frame popmid3V2Frame = new POPMID3V2Frame(email, rating, timesPlayed);
popmid3V2Frame.setPopularity(email, rating, timesPlayed);
if (ID3V2_3_0Tag != null) {
frames = ID3V2_3_0Tag.getPOPMFrames();
if (frames != null) {
if (frames.length > 0) {
String emailtouser[]=getmp3Email(SourceFile);
for (int i = 0; i < frames.length; i++) {
if (frames[i] != null) {
ID3V2_3_0Tag.removePOPMFrame(emailtouser[i]);
}
}
}
}
} else {
ID3V2_3_0Tag = new ID3V2_3_0Tag();
}
ID3V2_3_0Tag.addPOPMFrame(popmid3V2Frame);
MediaFile.setID3Tag(ID3V2_3_0Tag);
MediaFile.sync();
} catch (ID3Exception | OutOfMemoryError e) {
e.printStackTrace();
error = e.getMessage();
}
error = finish(context, SourceFile);
return error;
}
For anyone using the jid3 library, I added another method to the class MP3File.java
#Override
public int testforVerion() {
int iMinorVersion=0;
try {
InputStream oSourceIS = new BufferedInputStream(m_oFileSource.getInputStream());
ID3DataInputStream oID3DIS = new ID3DataInputStream(oSourceIS);
try
{
// check if v2 tag is present
byte[] abyCheckTag = new byte[3];
oID3DIS.readFully(abyCheckTag);
if ((abyCheckTag[0] == 'I') && (abyCheckTag[1] == 'D') && (abyCheckTag[2] == '3'))
{
// check which version of v2 tags we have
iMinorVersion = oID3DIS.readUnsignedByte();
}
else
{
return iMinorVersion;
}
}
finally
{
oID3DIS.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return iMinorVersion;
}
simply check for value 3 for ID3V2_3 and 4 for ID3V2_4
I have a typemap targetting Python which accepts both an already wrapped pointer object or additionally allows passing a Python sequence. In the case of a wrapped pointer, I do not want to delete the memory as SWIG owns it. However, when processing a sequence I'm allocating a temporary object that needs to be deleted. So I added a flag to my 'in' typemap to mark whether I allocated the pointer target or not. How can I access this flag in the corresponding 'freearg' typemap?
The typemaps look like this:
%typemap(in) name* (void* argp = 0, int res = 0, bool needsDelete = false) {
res = SWIG_ConvertPtr($input, &argp, $descriptor, $disown | 0);
if (SWIG_IsOK(res)) {
$1 = ($ltype)(argp); // already a wrapped pointer, accept
} else {
if (!PySequence_Check($input)) {
SWIG_exception(SWIG_ArgError(res), "Expecting a sequence.");
} else if (PyObject_Length($input) != size) {
SWIG_exception(SWIG_ArgError(res), "Expecting a sequence of length " #size);
} else {
needsDelete = true;
$1 = new name;
for (int i = 0; i < size; ++i) {
PyObject* o = PySequence_GetItem($input, i);
(*$1)[i] = swig::as<type>(o);
Py_DECREF(o);
}
}
}
}
%typemap(freearg) name* {
if ($1 /* && needsDelete */) delete $1;
}
This leads to code being generated that looks like:
{
res2 = SWIG_ConvertPtr(obj1, &argp2, SWIGTYPE_p_MyName_t, 0 | 0);
if (SWIG_IsOK(res2)) {
arg2 = (MyName *)(argp2); // already a wrapper pointer, accept
} else {
if (!PySequence_Check(obj1)) {
SWIG_exception(SWIG_ArgError(res2), "Expecting a sequence.");
} else if (PyObject_Length(obj1) != 3) {
SWIG_exception(SWIG_ArgError(res2), "Expecting a sequence of length ""3");
} else {
needsDelete2 = true;
arg2 = new MyName;
for (int i = 0; i < 3; ++i) {
PyObject* o = PySequence_GetItem(obj1, i);
(*arg2)[i] = swig::as<double>(o);
Py_DECREF(o);
}
}
}
}
if (arg1) (arg1)->someMember = *arg2;
resultobj = SWIG_Py_Void();
{
if (arg2 /* && needsDelete */) delete arg2;
}
According to 11.15 Passing data between typemaps from the SWIG manual:
You just need to use the variable as needsDelete$argnum in the freearg typemap.
I've this piece of code, which is called by a timer Update mechanism.
However, I notice, that the memory size of the application, while running, continuously increases by 4, indicating that there might be a rogue pointer, or some other issue.
void RtdbConnection::getItemList()
{
std::vector<CString> tagList = mItemList->getItems();
//CString str(_T("STD-DOL1"));
PwItemList* pil = mPwSrv->GetItemList();
CPwItem pw ;
for(auto it = tagList.begin(); it != tagList.end(); ++it)
{
pw = mPwSrv->GetItem(*it);
pil->AddItem(&(PwItem)pw);
}
pil->AddInfo(DB_DESC); //Description
pil->AddInfo(DB_QALRM); // Alarm Status
pil->AddInfo(DB_QUNAK); //UNACK status
pil->AddInfo(DB_AL_PRI); // Priority of the alarm tag
pil->ExecuteQuery();
int i = 0;
for (auto it = tagList.begin(); i < pil->GetInfoRetrievedCount() && it != tagList.end(); i+=4, it++)
{
//item = {0};
CString str(*it);
PwInfo info = pil->GetInfo(i);
CString p(info.szValue().c_str());
bool isAlarm = pil->GetInfo(i+1).bValue();
bool isAck = pil->GetInfo(i+2).bValue();
int priority = pil->GetInfo(i+3).iValue();
item = ItemInfo(str, p, isAlarm, isAck, priority);
//int r = sizeof(item);
mItemList->setInfo(str, item); // Set the details for the item of the List
}
delete pil;
pil = NULL;
}
I cannot seem to find a memory block requiring de-allocation here. Nor is there any allocation of memory when I step inside the following function :
mItemList->setInfo(str, item);
which is defined as :
void ItemList::setInfo(CString tagname, ItemInfo info)
{
int flag = 0;
COLORREF tempColour;
std::map<CString, ItemInfo>::iterator tempIterator;
if ( (tempIterator = mAlarmListMap.find(tagname)) !=mAlarmListMap.end() )
{
//remove the current iteminfo and insert new one
if(mAlarmListMap[tagname].getPriority() != info.getPriority() && (mAlarmListMap[tagname].getPriority()!=0))
{
mAlarmListMap[tagname].updatePriority(info.getPriority());
mAlarmListMap[tagname].mPrioChanged = TRUE;
}
else
{
mAlarmListMap[tagname].mPrioChanged = FALSE;
((mAlarmListMap[tagname].getPrevPriority() != 0)?(mAlarmListMap[tagname].ResetPrevPriority()):TRUE);
mAlarmListMap[tagname].setPriority(info.getPriority());
}
mAlarmListMap[tagname].setDescription(info.getDescription());
mAlarmListMap[tagname].setAlarm(info.getAlarmStat());
mAlarmListMap[tagname].setAlarmAck(info.getAckStat());
tempColour = mColourLogic->setUpdatedColour(mAlarmListMap[tagname].getAlarmStat(), mAlarmListMap[tagname].getAckStat(), flag);
mAlarmListMap[tagname].setColour(tempColour);
if(!(info.getAlarmStat() || info.getAckStat()))
{
flag = 1;
mAlarmListMap[tagname].mIsRTN = true;
mAlarmListMap[tagname].setDisplayCondition(false);
}
else
{
mAlarmListMap[tagname].setDisplayCondition(true);
}
//((mAlarmListMap[tagname].mIsRTN == true)?
}
else
{
tempIterator = mAlarmListMap.begin();
tempColour = mColourLogic->fillColourFirst(info.getAlarmStat(), info.getAckStat());
info.setColour(tempColour);
mAlarmListMap.insert(tempIterator, std::pair<CString,ItemInfo>(tagname,info));
}
}
I tried juggling with the allocations, but the increase is always a constant 4.
Could anyone kindly look and highlight where the issue could be?
Thanks a lot.