RWTPtrSlist core dumps, while removing entries.

N

Neha

#include <rw/tpslist.h>
#include <iostream>
using namespace std;

int main(){
RWTPtrSlist<int> dates;
dates.insert(new int(2)); // 6/2/52
dates.insert(new int(2)); // 6/2/52
// Remove in reverse order:
try
{
while (dates.get()); // When no more entries are there in
list.. it crashes..
}
catch(...)
{
cout << "here" <<endl;
}


return 0;
 
M

mlimber

Neha said:
#include <rw/tpslist.h>
#include <iostream>
using namespace std;

int main(){
RWTPtrSlist<int> dates;
dates.insert(new int(2)); // 6/2/52
dates.insert(new int(2)); // 6/2/52
// Remove in reverse order:
try
{
while (dates.get()); // When no more entries are there in
list.. it crashes..
}
catch(...)
{
cout << "here" <<endl;
}


return 0;

We don't know anything about RWTPtrSlist<> since it is not a standard
class. If you wrote it, show us that class and its implementation, or
if you didn't, ask the vendor of the library.

Cheers! --M
 
R

Roland Pibinger

#include <rw/tpslist.h>
#include <iostream>
using namespace std;

int main(){
RWTPtrSlist<int> dates;
dates.insert(new int(2)); // 6/2/52
dates.insert(new int(2)); // 6/2/52
// Remove in reverse order:
try
{
while (dates.get()); // When no more entries are there in
list.. it crashes..

The Rogue Wave library is off topic but the general approach how to
tackle such a problem is probably on topic:

1. Read the manual:
http://www.roguewave.com/support/docs/sourcepro/edition8/html/toolsref/rwtptrslist.html#idx3150

The function get() "Removes and returns the first element in the
collection". The description does not tell you that it checks for an
empty list and that it returns NULL in case of an empty list. So
probably you have to do that. Also your comment ("// Remove in reverse
order") is probably wrong.

2. Write a short unit test to verify your assumption:

RWTPtrSlist<int> dates;
dates.insert(new int(2));
dates.insert(new int(2));

int i = 0;

while (!dates.isEmpty()) {
int* p = dates.removeLast();
++i;
delete p;
}
assert (i == 2);
assert (dates.isEmpty());


BTW, both your pogram and Rogue Waves example contain memory leaks.

Best wishes,
Roland Pibinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top