Out of memory error for DB-querying script

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Out of memory error for DB-querying script

Out of memory error for DB-querying script

by sharevari » Sat Apr 14, 2012 9:13 am

Apologies for the barrage of questions, but I can't seem to get around this one.

I have written a VBS script which reads track metadata input from a text file and then proceeds to query the database for these tracks, one by one, using the SDB.Database.OpenSQL() function. Currently, I just log the output to another text file.

However, this script quickly makes the memory usage of MM shoot up until it either crashes or fires up several "Out of Memory" error dialogs. I suspected that I needed a corresponding Close statement for the OpenSql but can't seem to find such a thing in the API.

Interestingly enough though, the memory gets reclaimed every time I minimize the MM window. This leads me to think that it's not a memory leak in my script but rather something fishy in MM itself?

I'll include the main part of the script below:

Code: Select all

Sub main
 
  Dim artist, track, tag, genre
  Dim fso, fileName, logFileName, errFileName, file, logFile, errFile

  artist = ""
  track = ""
  tag = ""
  genre = ""
  
  Set fso = CreateObject("Scripting.FileSystemObject")
  fileName = "E:\Doks\Code\scripts\tracktags.txt"
  logFileName = "E:\Doks\Code\scripts\log.txt"
  errFileName = "E:\Doks\Code\scripts\err.txt"

  If fso.FileExists(fileName) Then
    
    Set file = fso.OpenTextFile(fileName, 1)
    Set logFile = fso.OpenTextFile(logFileName, 2, True)
    Set errFile = fso.OpenTextFile(errFileName, 2, True)

    Do While Not file.AtEndOfStream

      Dim line

      SDB.ProcessMessages()

      line = file.ReadLine

      If Trim(line) <> "" Then

        Dim colonPos

        colonPos = InStr(line, ":") - 1
        
        If StrComp(Left(line, colonPos), "track_artist") = 0 Then
          artist = Mid(line, colonPos + 2)
        ElseIf StrComp(Left(line, colonPos), "track_name") = 0 Then
          track = Mid(line, colonPos + 2)
        ElseIf StrComp(Left(line, colonPos), "track_tag") = 0 Then
          tag = Mid(line, colonPos + 2)
        ElseIf StrComp(Left(line, colonPos), "track_genre") = 0 Then
          genre = Mid(line, colonPos + 2)
        Else
          assert False, "Info line missing from triple: " & artist & " - " & title & ":" & tag & ":" & genre
        End If

      Else

        'Have we collected something in our variables?
        If StrComp(artist, "") <> 0 Then

          Dim sql, iter

          assert StrComp(track, "") <> 0, "Track not present"

          logFile.WriteLine("File data: " & artist & " - " & track & " [" & tag & "] [" & genre & "]")
          
          'If so, query the track and update it
          sql = "SELECT ID, Tempo, Genre, Artist, SongTitle, SongPath FROM Songs " & _
            "WHERE UPPERW(Artist) = UPPERW('" & escape(artist) & "') AND " & _
            "UPPERW(SongTitle) = UPPERW('" & escape(track) & "')"
          Set iter = SDB.Database.OpenSQL(sql)

          If iter.EOF Then
            logFile.WriteLine("ERROR: Not found in DB using SQL:")
            logFile.WriteLine(sql)

            errFile.WriteLine("Not found: " & artist & " - " & track)
            errFile.WriteLine("SQL      : " & sql)
            errFile.WriteLine("")
          End If
          
          While Not iter.EOF

            Dim id, tags, genres, dbArtist, dbTitle, dbPath
            
            id = iter.ValueByIndex(0)
            tags = iter.StringByIndex(1)
            genres = iter.StringByIndex(2)
            dbArtist = iter.StringByIndex(3)
            dbTitle = iter.StringByIndex(4)
            dbPath = iter.StringByIndex(5)

            logFile.WriteLine("DB data:   " & dbArtist & " - " & dbTitle & _
              " [" & tags & "] [" & genres & "] (" & id & ") " & dbPath)

            If StrComp(genre, "") = 0 Then 'We have a tag
              
              assert StrComp(tag, "") <> 0, "Neither tag nor genre present"

              If StrComp(tags, "") <> 0 Then
                tags = tags & "; " & tag  
              Else
                tags = tag  
              End If

              logFile.WriteLine("UPDATE Songs SET Tempo = '" & escape(tags) & "' WHERE ID = " & id)

            ElseIf StrComp(tag, "") = 0 Then 'We have a genre

              assert StrComp(genre, "") <> 0, "Neither tag nor genre present"

              'Let Last.fm tag genre override
              genres = PCase(genre)

              logFile.WriteLine("UPDATE Songs SET Genre = '" & escape(genres) & "' WHERE ID = " & id)
  
            End If            

            iter.Next()
          Wend
          
          Set iter = Nothing

          artist = ""
          track = ""
          tag = ""
          genre = ""

          logFile.WriteLine("")

        End If 'end if we have artist

      End If 'end if line is a blank line

    Loop
  
    file.Close
    logFile.Close
    errFile.Close

  End If

End Sub

Top